Association mappings: http://www.cnblogs.com/huxi/archive/2009/12/15/1624988.html
First, hibernate's loading process
Two ways to load a 1.Hibernate configuration file
①configuration configuration = new configuration ();
Configuration.configure ();
Public Configuration Configure () throws hibernateexception{
Configure ("/hibernate.cfg.xml");
return this;
}
②configuration configuration = new configuration ();
Configuration.configure ("");
Public Configuration Configure (String Resource) throws hibernateexception{
Log.info ("Configuring from Resource:" +resource);
InputStream stream = Getconfigurationinputstream (Resource);
Return Doconfig (Stream,resource);
}
Second, CRUD operations
1. Create a configuration file that loads hibernate from a configuration object (a mapping resource file that contains realm classes and tables in the config file)
2. Get the Sessionfactory object from the Configuration object
3. Open Session:sessionFactory.openSession ();
4. Use the session to query
5. Turn on transaction: Session.beagintransaction (); Curd operation. Commit the transaction and close the session;
1 Public voidtestsave{2Configuration Configuration =NewConfiguration ();3 //load the Hibernate.cfg.xml under the Classpath4 configuration.configure ();5 //Call Sessionfactory6Sessionfactory SF =configuratiion.buildsessionfactory ();7 //Open Sessioin8Session s =sf.opensession ();9 //Open TransactionTenTransaction TR =s.begintransaction (); One //to save the operation AObject obj =NewObject (); - Obj.set (); - ......... the s.save (obj); - tr.commit (); -S.close ()View Code
Third, the relationship operation
1. The State of the object
Temporary Status: New
Persistent state: Get Save Update
Off-tube Status: Clear Close evict
2, relational operations
2.1-to-one-and-one-to-many unidirectional association mappings
Many-to-one
12<className= "Bean. User "table=" T_user ">3<id name= "id" ><generatorclass= "Native"/></id>4<property name= "Name"/>5<!--to map a many-to-one relationship using "Many-to-one". A foreign key is automatically generated when you export the DDL--6<many-to-one name= "group" column= "GID"/>7 8</class>9Ten One<className= "Bean. Group "table=" T_group "> A<id name= "id" ><generatorclass= "Native"/></id> -<property name= "Name"/> -</class> theOne-to-many
12<className= "Bean. User "table=" T_user ">3<id name= "id" ><generatorclass= "Native"/></id>4<property name= "Name"/>5</class>678<className= "Bean. Group "table=" T_group ">9<id name= "id" ><generatorclass= "Native"/></id>Ten<property name= "Name"/> One<!--use Set mapping collection-- A<set name= "Users" > -<!--use "key" to specify foreign keys in the Foreign key table (T_user) that refer to itself-- -<key column= "gid"/> the<!--use one-to-many movies-- -<one-to-manyclass= "Bean. User "/> -</set> -</class> +2.2 Bidirectional correlation
<!--bean/user.hbm.xml--className= "Bean. User "table=" T_user "> <id name=" id "><generatorclass= "Native"/></id> <property name= "name"/> <!--use <many-to-one> to map a many-to-one relationship. A foreign key is automatically generated when you export the DDL--<many-to-one name= "group" column= "gid"/> </class>className= "Bean. Group "table=" T_group "> <id name=" id "><generatorclass= "Native"/></id> <property name= "name"/> <!--use <set> Map Collection--<set name = "Users" > <!--use <key> to specify foreign keys in the Foreign key table (T_user) referenced to itself--<key column= "gid"/> <!--using <one-to-many> mapping one-to-many relationships-<one-to-manyclass= "Bean. User "/> </set> </class>In a bidirectional association, adding "Inverse=true" to "set" can flip the maintenance relationship: Hibernate will discard the maintenance from one end. User and group relationships must use user maintenance, and hibernate will not maintain this relationship when the group is operating.
<set name= "Users" inverse=true>
Operations Group Example
sessiion.begintransation (); User User=NewUser (); User.setname ("Bla"); Grop Group=NewGroup (); Group.setname ("Admin"); Group.setusers (NewHashSet ()); Group.getusers (). Add (user), Session.save (user); Session.save (group); Session.gettransaction (). Commit ();//maintenance at one end after flipping oversession.begintransaction (); Group Group=NewGroup (); Group.setname ("Admin"); User User=NewUser (); User.setname ("Zhang San"); User.setgroup (group); Session.save (group); Session.save (user); Session.gettransaction (). Commit ()2.3 One-to-one correlation mappings
According to the mapping method, it can be divided into Primary Key association map and unique Foreign Key association mapping . The primary key association is to maintain the primary key consistency of the two tables and, if necessary, to add a foreign key constraint on the primary key; The unique foreign Key association is similar to a many-to-one association, which joins a foreign key column for a table, but a one-to-a association adds a unique constraint to the foreign key at the same time.
1. Primary Key correlation mapping
Primary key one-way Association
<!--bean/user.hbm.xml--class name= "Bean. User "table=" T_user "> <id name=" id "> <!--Specify the primary key generation policy as foreign key---<generator class = "foreign" > <!--Specify properties to reference-<param name= "PR Operty ">group</param> </generator> </id> <property name=" name "/> <!--use <one-to-one> map one-to-one relationships. --<one-to-one name= "group" > </class >
<!--bean/group.hbm.xml --class name= "beans. Group "table=" T_group "> class=" Native "/></id> <property name=" name "/> </class>
Primary KEY Bidirectional Association
<!--bean/user.hbm.xml--class name= "Bean. User "table=" T_user "> <id name=" id "> <!--Specify the primary key generation policy as foreign key---<generator class = "foreign" > <!--Specify properties to reference-<param name= "PR Operty ">group</param> </generator> </id> <property name=" name "/> <!--use <one-to-one> map one-to-one relationships. --<one-to-one name= "group" > </class >
<!--bean/group.hbm.xml --class name= "beans. Group "table=" T_group "> class=" Native "/></id> <property name=" name "/> <!--use <one-to-one> map one-to-one relationships- <one-to-one name= "user"/> </class></ Hibernate-mapping>
2. Unique FOREIGN key correlation mapping
Unidirectional
<!--bean/user.hbm.xml --class name= "beans. User "table=" T_user "> <id name=" id "> class=" native "/> </id> < Property name= "Name"/> <!--<many-to-one> Plus unique becomes one-to-one- <many-to-one name= "group" Unique= "true" column= "gid"/> </class>
<!--bean/group.hbm.xml --class name= "beans. Group "table=" T_group "> class=" Native "/></id> <property name=" name "/> </class>
Bidirectional
<!--bean/user.hbm.xml--class name= "Bean. User "table=" T_user "> <id name=" id "> <generator class =" n Ative "/> </id> <property name=" name "/> <!--<many-to-one> Plus unique becomes a one-to-one--& Gt <many-to-one name= "group" unique= "true" column= "gid"/> </class >
<!--bean/group.hbm.xml --class name= "beans. Group "table=" T_group "> class=" Native "/></id> <property name=" name "/> <!--using <one-to-one> mapping one-to- <one-to-one name= "user"/> </class></ Hibernate-mapping>
2.4 Many-to-many associations
One-Way Association
className= "Bean. User "table=" T_user "> <id name=" id "> <generatorclass= "Native"/> </id> <property name= "name"/> <!--using the Set mapping collection, Hibernate will generate a third table in a many-to-many relationship --<set name= "groups" table= "T_user_group" > <!--use key to specify foreign keys in the Foreign key table (T_user_group) referenced to itself-- <key column= "userid" ></key> <!--use Many-to-one to map a many-to-many relationship, column specifies the other end of the columns in the table T_user_group- <many-to-manyclass= "Bean. Group "colun=" GroupID "></many-to-many> </set> </class>className= "Bean. Group "table=" T_group "> <id name=" id "><generatorclass= "Native"/></id> <property name= "name"/> </class>Bidirectional correlation
<!--bean/user.hbm.xml --class name= "beans. User "table=" T_user "> <id name=" id "> class=" native "/> </id> < Property name= "Name"/> <!--use <set> Map collection, in a many-to-many relationship, Hibernate will generate a third table-- <set name= "groups" table= "T_user_group" > <!--use <key> specify foreign keys in the Foreign key table (T_user_group) referenced to itself-- <key column= " UserID "/> <!--use <many-to-many> map Many-to-many relationships, column specifies the other end in table T_user_group-- class = "Bean. Group "column=" GroupID "/> </set> </class>
<!--bean/group.hbm.xml --class name= "beans. Group "table=" T_group "> <id name=" id "> class=" native "/> </id> < Property name= "Name"/> <!--use <set> Map collection, Hibernate will generate a third table-- <set name= "Users" in many-to-many relationships table= "T_user_group" > <!--use <key> to specify foreign keys in the Foreign key table (T_user_group) referenced to itself-- <key column= "group "/> <!--use <many-to-many> map Many-to-many relationships, column specifies the other end in table T_user_group-- class=" Bean. User "column=" userid "/> </set> </class>
A many-to-many bidirectional association can also set inverse= "true" in <set> on one end of the relationship that you do not want to maintain, but there must be a side that can be maintained, that is, you can only set a single.
Iv. Benefits of Hibernate
V. HQL statements
Hibernate---Progress 1