Hibernate simple examples and notes

Source: Internet
Author: User



First create the person class with the following code: (Person.java)

Import java.util.*;

public class Person {

public int id;

public String name;

public String password;

public Date birthday;

Public person () {

}

Public person (string name, string password, Date birthday) {

Super ();

THIS.name = name;

This.password = password;

This.birthday = Birthday;

}

@Override

Public String toString () {

return "person [id=" + ID + ", name=" + name + ", password=" + password

+ ", birthday=" + Birthday + "]";

}

public int getId () {

return ID;

}

public void setId (int id) {

This.id = ID;

}

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

Public String GetPassword () {

return password;

}

public void SetPassword (String password) {

This.password = password;

}

Public Date Getbirthday () {

return birthday;

}

public void Setbirthday (Date birthday) {

This.birthday = Birthday;

}

}

Create a map of the person with the following code: (Person.hbm.xml)

<?xml version= "1.0"?>

<! DOCTYPE hibernate-mapping Public

"-//hibernate/hibernate Mapping DTD 3.0//en"

"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >


<!--Configure the class to be mapped--

<class name= "person" table= "person" >

<id name= "id" column= "id" >

<generator class= "native"/>

</id>

<!--column If it is not written, it is automatically created according to the value in name, and if it already exists, it is automatically mapped.

<property name= "Name" ></property>

<property name= "password" column= "password" ></property>

<property name= "Birthday" column= "Birthday" ></property>

</class>



Configure the hibernate file with the following code: (Hibernate.cfg.xml)

<! DOCTYPE hibernate-configuration Public

"-//hibernate/hibernate Configuration DTD 3.0//en"

"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >


<session-factory>

<!--the values of those name properties can be copied from the Hibernate.properties copy in the Hibernate package, depending on the database type, with different statements--

<!--database dialect, which is what makes the database the biggest feature-

<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>

<!--loading a database drive

<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>

<!--the database to connect to, which is equivalent to getting a connection--

<property name= "Hibernate.connection.url" >jdbc:mysql:///hibernatedemo</property>

<!--Database Account--

<property name= "Hibernate.connection.username" >root</property>

<!--database Password--

<property name= "Hibernate.connection.password" >123456</property>

<property name= "Show_sql" >true</property>

<!--If the table does not exist, add this sentence to automatically create a table--

<!--

Create-drop create the corresponding data table structure each time you start, delete the corresponding table structure when sessionfactory close

Create each time you start, delete the last created table structure and recreate a new table structure

Validate verifies and modifies the table structure each time it is started

-

<property name= "Hibernate.hbm2ddl.auto" >update</property>

<!--map, the path of this resource must be written correctly, to write the package name, you can right-click on the file, select roperties can see the path, the src behind the copy paste it up--

<mapping resource= "Person.hbm.xml"/>

</session-factory>


Create the test class, which is used primarily to test each function, with the following code: (Test.java)

Import org.hibernate.*;

Import org.hibernate.cfg.*;

Import Org.hibernate.metamodel.source.annotations.entity.ConfiguredClass;

Import Org.hibernate.service.ServiceRegistry;

Import Org.hibernate.service.ServiceRegistryBuilder;

Import org.hibernate.service.spi.ServiceBinding;


public class Test {

public static void Main (string[] args) {

Test ();


Sava ();


Get ();


Load ();


Update1 ();


Update2 ();


Merge1 ();


Merge2 ();


}



private static void Merge2 () {

Configuration config = new configuration (). Configure ();

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Transaction tx = Session.begintransaction ();

person P1 = new person ();

P1.setid (4);

P1.setname ("TTTMM");

person P2 = (person) session.merge (p1);

P1.setname ("Uuudhaf");

P2.setname ("KKKKJJSS");

Tx.commit ();

Session.close ();

}


private static void Merge1 () {

Configuration config = new configuration (). Configure ();

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Transaction tx = Session.begintransaction ();

person P1 = (person) session.load (Person.class, 3);

P1.setname ("Jjoo");

person P2 = (person) session.merge (p1);

P1.setname ("Oojj");

P2.setname ("Qqww");


P2.setname ("Qqww");

P1.setname ("Oojj");


Tx.commit ();

Session.close ();

}


private static void Update2 () {

Configuration config = new configuration (). Configure ();

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Transaction tx = Session.begintransaction ();

person P1 = new person ();

P1.setname ("Ooxx");//The same time, because P1 is a temporary state, so the name does not change at this time

P1.setid (4);

Session.update (p1);

P1.setname ("Xxoo");

Tx.commit ();

Session.close ();

}


private static void Update1 () {

Configuration config = new configuration (). Configure ();

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Transaction tx = Session.begintransaction ();

person P1 = (person) session.get (Person.class, 3);

P1.setname ("Tom"); When this sentence is finished, the name in the database has become Tom. This is a function of the persistent state.

Session.update (p1);

P1.setname ("Lalala");

At this point the name in the database becomes Lalala, that is, the real-time changes mentioned above, but also can be seen as a database of a day record of the reference,, a change will change.

Tx.commit ();

Session.close ();

}


private static void Load () {

Configuration config = new configuration (). Configure ();

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Person P = (person) session.load (Person.class, 3);

SYSTEM.OUT.PRINTLN ("Load----->" + P);

Session.close ();

}


private static void Get () {

Configuration config = new configuration (). Configure ();

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Person P = (person) session.get (Person.class, 1);

SYSTEM.OUT.PRINTLN ("Get----->" + P);

Session.close ();

}


private static void Sava () {

Person p = new person ("KKJJ", "123456", New Java.util.Date ());

Configuration config = new configuration (). Configure ();

This is a new method of acquiring Sessionfactory in Hibernate4, which is more efficient and safer than the old method.

serviceregistry sr = new Serviceregistrybuilder (). Applysettings (

Config.getproperties ()). Buildserviceregistry ();

Sessionfactory factory = Config.buildsessionfactory (SR);

Session session = Factory.opensession ();

Transaction tx = Session.begintransaction ();

Session.save (P);

Tx.commit ();

Session.close ();

}


private static void Test () {

Person p = new person ("KK", "123", New Java.util.Date ());

Reading configuration Files

Configuration cfg = new configuration (). Configure ();

Hibernate's Core interface

The following method to obtain Sessionfactory has been eliminated, in Hibernate4 has been used in another way.

Sessionfactory factory = Cfg.buildsessionfactory ();

Session session = Factory.opensession ();

Open transaction

Transaction tx = Session.begintransaction ();



Session.save (P);

Submit, similar to execute it, can be understood as excetuupdate ()

Tx.commit ();

Session.close ();

}

}



This article is from "Little Kurchis" blog, please be sure to keep this source http://773478496.blog.51cto.com/7382353/1584571

Hibernate simple examples and notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.