Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Hibernate4 The Hello World (basic environmental structures)     - Linux development environment to build and use the directory structure and file --Linux (Linux)

- RAID configuration and management under linux (Server)

- The ActiveMQ JMS installation and application examples for Linux (Linux)

- Timing Nginx logs cut and remove the log records of the specified number of days before (Server)

- To obtain installation package (RPM) under RHEL6 (Linux)

- Using BBED repair ORA-01190 error (Database)

- 64-bit Ubuntu 15.10 How to compile the latest version of the 32 Wine 1.7.53 (Linux)

- AngularJS application unit testing started (Programming)

- Using Vagrant to build multi-platform environment (Server)

- Setting Hibernate connection MySQL database connection pool coding (Database)

- Analysis examples: Intrusion Response Linux platform Case (Linux)

- Zabbix installation and configuration process (Server)

- linux system optimization and security configuration (Linux)

- Elaborate .NET Multithreading: Using Task (Programming)

- Java in the final qualifier (Programming)

- Linux file content inspection - cat, tac, no, more, less, head, tail, od (Linux)

- Oracle online redefinition (Database)

- Permissions and attributes of files and directories under Linux (Linux)

- Linux daemon (Linux)

- Hutchison DG standby database CPU consumption reached bottleneck repair (Database)

 
         
  Hibernate4 The Hello World (basic environmental structures)
     
  Add Date : 2018-11-21      
         
         
         
  At first review Hibernate, just reviewed, we found all forgotten, and even the environment to build will not equal all over again ah, no way to bite the bullet and had to start all over again.

Hibernate is an excellent ORM framework, object relation mapping object-relational mapping. My understanding is automatically put into operation pojo class of objects in the corresponding operations on the database tables. It simply is to create a pojo class object, then the corresponding database table will insert such an object. Modify, delete, of course, of the. It is to be understood as isolated java database operation and Development.

First, download the Hibernate jar package

You find the necessary Hibernate version http://www.Hibernate.org/ in. I downloaded the latest Hibernate-release-4.3.1.Final.

Second, copy the jar package

Copy in lib / required to package all jar lib folder of the project under

Third, the deployment Hibernate.cfg.xml, corresponding pojo of Student.hbm.xml

New Hibernate.cfg.xml file in the src directory of the project. In the documentation / manual / en-US / html_single / index.html

< ? Xml version = '1.0' encoding = 'utf-8'?>
< ! DOCTYPE hibernate-configuration PUBLIC
        "- // Hibernate / Hibernate Configuration DTD 3.0 // EN"
        "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

< Hibernate-configuration>

    < Session-factory>

        < -! Database connection settings ->
        < Property name = "connection.driver_class"> Oracle.jdbc.driver.OracleDriver < / property>
        < Property name = "connection.url"> jdbc: oracle: thin: @ 127.0.0.1: 1521: lubby < / property>
        < Property name = "connection.username"> admin < / property>
        < Property name = "connection.password"> admin < / property>

    
        < -! SQL dialect ->
        < Property name = "dialect"> org.hibernate.dialect.Oracle10gDialect < / property>

      
        < -! Disable the second-level cache ->
        < Property name = "cache.provider_class"> org.hibernate.cache.internal.NoCacheProvider < / property>

        < -! Echo all executed SQL to stdout ->
        < Property name = "show_sql"> true < / property>

        < -! Drop and re-create the database schema on startup ->
        < -! < Property name = "hbm2ddl.auto"> update < / property> ->

        < Mapping resource = "com / lubby / pojo / Student.hbm.xml" />

    < / Session-factory>

< / Hibernate-configuration>

The first four labels are driven configuration database address and user name password.

 < -! SQL dialect ->
        < Property name = "dialect"> org.hibernate.dialect.Oracle10gDialect < / property>

This is the name and version of the configuration database. In the documentation / manual / en-US / html_single / index.html can be found in the corresponding database field corresponding I use the oracle 11g is written above the corresponding

      < -! Echo all executed SQL to stdout ->
        < Property name = "show_sql"> true < / property>

This is to choose whether to display the sql statement.

 < Mapping resource = "com / lubby / pojo / Student.hbm.xml" />

This is very important to tell hibernate configuration file configuration file configured pojo class address

Fourth, create a class Student pojo

package com.lubby.pojo;

public class Student {
 private String sid;
 private String sname;
 private int age;
 public Student () {
  super ();
  // TODO Auto-generated constructor stub
 }
 public Student (String sid, String sname, int age) {
  super ();
  this.sid = sid;
  this.sname = sname;
  this.age = age;
 }
 public String getSid () {
  return sid;
 }
 public void setSid (String sid) {
  this.sid = sid;
 }
 public String getSname () {
  return sname;
 }
 public void setSname (String sname) {
  this.sname = sname;
 }
 public int getAge () {
  return age;
 }
 public void setAge (int age) {
  this.age = age;
 }
 @Override
 public String toString () {
  return "Student [sid =" + sid + ", sname =" + sname + ", age =" + age
    + "]";
 }
 
}

Fifth, the configuration Student.hbm.xml

Pojo Each class has a corresponding profile. They are placed inside the package pojo class resides.

< ? Xml version = "1.0"?>
< ! DOCTYPE hibernate-mapping PUBLIC
        "- // Hibernate / Hibernate Mapping DTD 3.0 // EN"
        "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

< Hibernate-mapping package = "com.lubby.pojo">
 < Class name = "Student" table = "tab_stu">
  < Id name = "sid" column = "sid"> < / id>
  < Property name = "sname" column = "sname"> < / property>
  < Property name = "age" column = "age"> < / property>
 < / Class>
< / Hibernate-mapping>

< Class name = "Student" table = "tab_stu"> < / class>

name: the name of the class

table: the name of the corresponding database table

< Id name = "sid" column = "sid"> < / id>

id: the primary key is set pojo class, column is the primary key of the corresponding database name. If you do not write the column, and the default name of the same content.

< Property name = "sname" column = "sname"> < / property>
< Property name = "age" column = "age"> < / property>

property: Setting the general properties

Sixth, call hibernate

package com.lubby.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jboss.weld.logging.messages.EventMessage;

import com.lubby.pojo.Student;

public class StudentTest {
 
 public static void main (String args []) {
  Student stu = new Student ();
  stu.setSid ( "3");
  stu.setAge (25);
  stu.setSname ( "Li Huitang");
  Configuration cfg = new Configuration ();
  SessionFactory sf = cfg.configure () buildSessionFactory ().;
  Session session = sf.openSession ();
  session.beginTransaction ();
  session.save (stu);
  session.getTransaction () commit ().;
  session.close ();
  sf.close ();
 
 }
}
     
         
         
         
  More:      
 
- CentOS6.0 successful installation and configuration OpenCV (Linux)
- configuration ssh without password under Linux (Linux)
- CentOS install Memcached (Server)
- The best known for archiving / compression tool under linux (Linux)
- VSFTPD Security (Linux)
- Linux system security configuration (Linux)
- Linux System Getting Started Tutorial: How to update outdated version of Ubuntu (Linux)
- MySQL optimization tabs (Database)
- Java environment to build a number of issues (Linux)
- Java multi-threaded communications pipeline flow (Programming)
- Linux operation and maintenance of the automated installation and deployment RHEL7.0 Cobbler (Linux)
- To teach you how to safely send mail (Linux)
- Archive log file size and redo log file size relationship (Database)
- IO reference Docker container (Server)
- Linux Oracle environment variable is invalid Problem Solving (Database)
- Linux Creating a new user error Creating mailbox file: File exists (Linux)
- Linux how to view your JDK installation (Linux)
- NIC configuration parameters under Linux (Linux)
- jQuery get value drop-down list and select text (Programming)
- How to install and use the malware detection tool LMD and ClamAV antivirus engine on Linux (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.