In terms of query and access rate for a pair of more than a few, here write only the main configuration file code
The first is Hibernate's configuration file
<!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hi Bernate.org/dtd/hibernate-configuration-3.0.dtd "> <hibernate-configuration> <session-factory> < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Hibernate.connection.url">Jdbc:mysql:///webproject</ Property> < Propertyname= "Hibernate.connection.username">Root</ Property> < Propertyname= "Hibernate.connection.password">123456</ Property> < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQL5Dialect</ Property> < Propertyname= "Hibernate.show_sql">True</ Property> < Propertyname= "Hibernate.hbm2ddl.auto">Create</ Property> <MappingResource= "Com/shaoxin/entity/dept.hbm.xml"/> <MappingResource= "Com/shaoxin/entity/employee.hbm.xml"/> </session-factory> </hibernate-configuration>
One-to-many configuration file code, which is clearly used in the set set
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hi Bernate-mapping-3.0.dtd "> <hibernate-mapping Package= "Com.shaoxin.entity"> <classname= "Dept"Table= "Dept"> <IDname= "DeptID"column= "DeptID"> <Generatorclass= "Native"></Generator> </ID> < Propertyname= "Deptname"column= "Deptname"></ Property><!--One -to-many set foreign key mode, class for foreign key type -<Setname= "Setemployees"Table= "Employee"> <Keycolumn= "dept_id"></Key> <One-to-manyclass= "Employee"/> </Set> </class> </hibernate-mapping>
The corresponding entity class:
Packagecom.shaoxin.entity;ImportJava.util.HashSet;ImportJava.util.Set; Public classDept {Private intDeptID; PrivateString Deptname; PrivateSet<employee> setemployees =NewHashset<employee>(); Public intGetdeptid () {returnDeptID; } Public voidSetdeptid (intDeptID) { This. DeptID =DeptID; } PublicString Getdeptname () {returnDeptname; } Public voidsetdeptname (String deptname) { This. Deptname =Deptname; } PublicSet<employee>getsetemployees () {returnsetemployees; } Public voidSetsetemployees (set<employee>setemployees) { This. Setemployees =setemployees; }}
Many-to-one profile code, which is obviously an object type
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "> <hibernate-mapping Package= "Com.shaoxin.entity"> <classname= "Employee"Table= "Employee"> <IDname= "EmployeeID"column= "EmployeeID"> <Generatorclass= "Native"></Generator> </ID> < Propertyname= "EmployeeName"column= "EmployeeName"/><!--Many-to-one create foreign key mode: attributes are: foreign key corresponding table, foreign key field, foreign key type -<Many-to-onename= "Dept"column= "dept_id"class= "Dept"></Many-to-one> </class> </hibernate-mapping>
The corresponding entity class
Packagecom.shaoxin.entity; Public classEmployee {Private intEmployeeID; PrivateString EmployeeName; PrivateDept Dept; Public intGetemployeeid () {returnEmployeeID; } Public voidSetemployeeid (intEmployeeID) { This. EmployeeID =EmployeeID; } PublicString Getemployeename () {returnEmployeeName; } Public voidsetemployeename (String employeename) { This. EmployeeName =EmployeeName; } PublicDept getdept () {returnDept; } Public voidsetdept (Dept Dept) { This. Dept =Dept; }}
Test the two storage methods
Packagecom.shaoxin.entity;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;Importorg.hibernate.classic.Session;Importorg.junit.Test; Public classTestmany2many {Staticsessionfactory SF; Static{SF=NewConfiguration (). Configure (). Buildsessionfactory (); } @Test Public voidTestone2many ()throwsException {Session opensession=sf.opensession (); Transaction BeginTransaction=opensession.begintransaction (); Employee Empzs=NewEmployee (); Employee Empls=NewEmployee (); Empls.setemployeename ("John Doe"); Empzs.setemployeename ("Zhang San"); Dept Dept=NewDept (); Dept.getsetemployees (). Add (EMPLS); Dept.getsetemployees (). Add (Empzs); Dept.setdeptname ("Application Development"); Opensession.save (EMPLS); Opensession.save (Empzs); Opensession.save (dept); Begintransaction.commit (); Opensession.close (); } @Test Public voidTestmany2one ()throwsException {Session opensession=sf.opensession (); Transaction BeginTransaction=opensession.begintransaction (); Employee Empzs=NewEmployee (); Employee Empls=NewEmployee (); Dept Dept=NewDept (); Dept.setdeptname ("Application Development"); Empls.setemployeename ("John Doe"); Empzs.setemployeename ("Zhang San"); Empls.setdept (dept); Empzs.setdept (dept); Opensession.save (dept); Opensession.save (EMPLS); Opensession.save (Empzs); Begintransaction.commit (); Opensession.close (); }}
Hibernate beginner one-to-many and multi-pair configuration and use