Relationship between employees and departments: if an employee can only belong to one department, the relationship between the employee and the Department is N: 1. If the employee can only find the department to which the employee belongs, instead of finding the corresponding employee from the Department, this becomes a one-way N: 1 Association.
Hibernate tool (simple test)
Public Class Hibernateutil { Private Static Final Sessionfactory = Buildsessionfactory (); Private Static Sessionfactory buildsessionfactory (){ Try { // Create the sessionfactory from hibernate. cfg. xml Return New Configuration (). Configure (). buildsessionfactory ();} Catch (Exception ex ){ // Make sure you log the exception, as it might be swallowed System. Out. println ("Initial sessionfactory creation failed." + Ex ); Throw New Exceptionininitializererror (Ex );}} Public Static Sessionfactory getsessionfactory (){ Return Sessionfactory ;} Public Static Session getsession () {sessionfactory SF = Getsessionfactory (); Return SF. opensession ();}}
Employee
Public class employee {private long ID; private string name; private department Department; public long GETID () {return ID;} public void setid (long ID) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public department getdepartment () {return Department;} public void setdepartment (Department) {This. department = Department ;}}
Employee configuration file
<? XML version = "1.0" encoding = "UTF-8" ?> <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 //" http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > < Hibernate-Mapping > < Class Name = "Com. longtech. hibernate. domain. Employee" Table = "Employee" > < ID Name = "ID" Column = "ID" Type = "Java. Lang. Long" > < Meta Attribute = "Field-description" > Unique Identifier </ Meta > < Generator Class = "Native" /> </ ID > < Property Name = "Name" Type = "Java. Lang. String" Column = "Name" Length = "20" > < Meta Attribute = "Field-description" > Employee name </ Meta > </ Property > < Allow-to-one Name = "Department" Column = "Dept_id" Cascade= "All" > </ Allow-to-one > </ Class > </ Hibernate-Mapping >
Department
Public Class Department { Private Long ID; Private String name; Public Long GETID (){ Return ID ;} Public Void Setid (long ID ){ This . ID = ID ;} Public String getname (){ Return Name ;} Public Void Setname (string name ){ This . Name = Name ;}}
Department configuration file
<? XML version = "1.0" encoding = "UTF-8" ?> <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 //" http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > < Hibernate-Mapping > < Class Name = "Com. longtech. hibernate. domain. Department" Table = "Department" > < ID Name = "ID" Column = "ID" Type = "Java. Lang. Long" > < Meta Attribute = "Field-description" > Unique Identifier </ Meta > < Generator Class = "Native" /> </ ID > < Property Name = "Name" Type = "Java. Lang. String" Column = "Name" Length = "20" > < Meta Attribute = "Field-description" > Department name </ Meta > </ Property > </ Class > </ Hibernate-Mapping >
Persistence class
Department dept =NewDepartment (); Dept. setname ("Department"); Employee emp1=NewEmployee (); emp1.setname ("Emp1"); Emp1.setdepartment (Dept); Session. Save (emp1 );
In the persistence class, when session. Save (emp1) is executed, if cascade = "all" is not specified, the org. hibernate. transientobjectexception exception is thrown during execution.
After the preceding operations, you can find thatProgramThe database generates two records.
The console will enter two records:
Hibernate: insert into Department (name) values (?) Hibernate: insert into employee (name, dept_id) values (?, ?)
If cassade = "all" is used, the database inserts the department record first, and then inserts the employee record. Otherwise, it reports an exception. In fact, this logic is the same as that used to operate databases through JDBC. If the Department is not inserted first, the foreign key cannot be referenced.