Save the collection of objects in Hibernate

Source: Internet
Author: User

First, in the Java Web design often use objects to manipulate, in Hibernate in the collection of objects save (one-to-many)

1 The following steps are required:

1) Design data table relationships

2) Introduction of jar package, need to pay attention to the introduction of database Connector

3) Realistic Body type

4) Configure the mapping file and Hibernate.cfg.xml

2 examples are as follows:

Employee-to-department relationships, multiple employees belong to one department, and one department has multiple employees. You can set a foreign key on the employee table to resolve a one-to-many relationship.

2.1) Create a table

CREATE TABLE employee (ID INT PRIMARY KEY auto_increment,ename VARCHAR () not null,depid INT, FOREIGN KEY (depid) REFERENCES Dept (ID) on DELETE CASCADE); CREATE TABLE Dept (id INT PRIMARY KEY auto_increment,dname VARCHAR);

2.2) Introduction of JAR Package

2.3) Realistic Body type

Employee class

 Public classEmployee {Private intID; PrivateString ename; PrivateDept Dept =NewDept ();  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString Getename () {returnename; }     Public voidsetename (String ename) { This. ename =ename; }     PublicDept getdept () {returnDept; }     Public voidsetdept (Dept Dept) { This. Dept =Dept; }        }
View Code

Department category

 Public classDept {Private intID; PrivateString dname; PrivateSet<employee> employees =NewHashset<employee>();  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString Getdname () {returndname; }     Public voidsetdname (String dname) { This. dname =dname; }     PublicSet<employee>GetEmployees () {returnemployees; }     Public voidSetemployees (set<employee>employees) {         This. Employees =employees; }}
View Code

2.4) Configure the mapping file

Departmental mapping file, Dept.hbm.xml

Mapping Package="com.baidu.entity"> <className="Dept"table="Dept"> <id name="ID"column="ID"> <generatorclass="native"/> </id> <property name="dname"column="dname"></property> <SetName="Employees"table="Emploee"> <key column="Depid"></key> <one-to-manyclass="Employee"/> </Set> </class>

Note: When configuring a one-to-many, the department is a pair of employees, the content set to be configured

1) which is the set property, employees

2) The collection property stores the table corresponding to the object, employee

3) foreign key of the table, Depid

4) The type of object stored in the collection, Class=employee

Employee mapping file, Employee.hbm.xml

Mapping Package="com.baidu.entity"> <className="Employee"table="Employee"> <id name="ID"column="ID"> <generatorclass="native"/> </id> <property name="ename"column="ename"></property> <many-to-one name="Dept" class="Dept"column="Depid"></many-to-one> </class>

Note: When configuring many-to-one, employees are more than one to the department. The content to configure Many-to-one

1) which object to map

2) which foreign key in the table is associated with the object

3) Types of mapped objects

Global configuration Hibernate.cfg.xml, you can view the previous blog hibernate development process

3 Simple test

 Public voidfun1 () {Configuration Configuration=NewConfiguration ();                Configuration.configure (); Sessionfactory SESSIONFAC=configuration.buildsessionfactory (); Session Session=sessionfac.opensession (); Transaction BT=session.begintransaction (); Dept Dept=NewDept (); Dept.setdname ("Test"); Employee Employee=NewEmployee (); Employee.setename ("test3");                        Employee.setdept (dept); Employee Employee2=NewEmployee (); Employee2.setename ("test4");                Employee2.setdept (dept);        Session.save (employee);        Session.save (EMPLOYEE2);                Session.save (dept);                Bt.commit ();        Session.close ();            Sessionfac.close (); }

It is necessary to note that

In the table is a foreign key to express a one-to-many or many pairs of relations, in the class is expressed through the collection of objects, such as Set/list/map; in the configuration file you need to be aware of the Set/list/map configuration

Which table has a foreign key to manipulate the table corresponds to that object to add another object, otherwise it will be problematic.

In hibernate you want to map an object into a foreign key field, only Many-to-one

Save the collection of objects in Hibernate

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.