Hibernate beginner one-to-many and multi-pair configuration and use

Source: Internet
Author: User
Tags set set

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

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.