Chuan Chi hibernate26-31
Employee emp1 = new Employee ();
emp2.setName (“emp name2″);
s.close ();
}
s.save (depart); / / four update update statement
*** inverse =” true “in the orderly collection, such as list, array array is not feasible, should be avoided.
emps.add (emp1);
t2.setStudents (ss);
cascade and inverse (Employee-Department)
Modify Department.hbm.xml
Table II sales, the PK, FK1 as employee_id, subsidiaries sell
s.save (emp2);
tx = s.beginTransaction ();
if (s! = null)
update Employee set depart_id =? where id =?
depart.setEmps (emps);
on-discriminator-value value, the error Could not format discriminator value to SQL string
}
} finally {
set>
** Dos command mysql – use test; – show tables;
emp1.setDepart (depart);
/ / Hibernate.initialize (emp.getDepart ());
Console display:
select skiller0_.emp_id as id2_0_, skiller0_1_.name as name2_0_, skiller0_1_.depart_id as depart3_2_0, skiller0_skill as skill3_0_ from skiller skiller0_ inner join Employee skiller0_1_ on skiller0_.emp_id = skiller0_1_.id where skiller0_.emp_id =?
inheritance mapping
*** object model needs to know the way that the department staff, department staff know the relational model / relational database only way.
class>
—————- —————— 28 ordered collection of analysis about the inverse attribute
Console display:
return emp;
try {
first two maintaining relationships, unlike many foreign keys [update], many to many in the middle of the table into the record; after the two is no different. Half of duplicate records, duplicate primary key violated conflict
2011 年 08 月 08 日
Session s = null;
each subclass table (joined-subclass) (table structure )
Employee emp = query (1); / / 2 if it
tx = s.beginTransaction ();
above static Employee query (int empId) {Employee emp = (Employee) s.get (Skiller.class, empId);} results
hibernate-mapping>
Skiller emp2 = new Skiller ();
s = HibernateUtil.getSession ();
class>
emp3. setName (“emp name3″);
if cancellation s.save (depart); is an error. Because it is an unsaved transient instatnce
shared information on the Employee, in particular information on the skiller or sales in
Set
— ——————————– 27 attributes about the role and principles of inverse analysis [for students to remember the teacher, the teacher do not bother]
xml version = "1.0"?>
insert into Department (name) values ??(?)
private int sell;
id>
Employee emp1 = new Employee ();
/ / the employee depart_id update to the department id
Transaction tx = null;
*** in inverse = ” false “under the influence of s.save (depart); s.save (emp1); s.save (emp2); there are two update statements, it is because persistent state after the change of the properties of the object (id has a value), hibernate know that this change is reflected in the database, two update update statement
inverse = “true” only exists many, many to many there, that is effective
depart.setName (” depart name “);
For the above in this “department know that staff, the staff know the department,” the maintenance, too complicated. Many of the one in the end use the inverse attribute
Employee emp = (Employee) s.get (Employee.class, empId ); / / if Employee.class into Skiller.class
}
s.save (emp1);
insert into skiller (skill, emp_id) values ??(?,?)
console display:
Note: configured for one-to-one object does not maintain the association
select employee0_.id as id2_0_ , employee0_.name as name2_0_, employee0_.depart_id as depart3_2_0_, employee0_1_.skill as skill3_0_, employee0_2.sell as sell4_0_, case when employee0_1.emp_id is not null then 1 when employee0_2.emp_id is not null then 2 when employee0_id is not null then 0 end as clazz_0_ from Employee employee0_left outer join skiller employee0_1_ on employee0_.id = employee0_1_.emp_id left outer join sales employee0_2_ on employee0_.id = employee0_2_.emp_id where employee0_.id =?
Department depart = add (); < br /> modify Department.hbm.xml
s = HibernateUtil.getSession ();
}
console display:
————————- ———- 30 about inheritance each class is mapped to a table joined-subclass table
modify Employee.hbm.xml
Table II, the primary key and foreign key to employee_id sub sell
public static void main (Stirng [] args) {
s.save (emp3);
*** hibernate does not allow many clients to give up to maintain the relationship, many end of the maintenance of high efficiency
t1.setStudents (ss);
package cn.itcast.hibernate.domain;
< key column = "depart_id" />
cascade and relationship maintenance
xml version = "1.0"?>
public class Skiller extends Employee {
emp1.setDepart (depart); / / object model: create two objects of association. Role for the update statement
s.save (emp1); / / first insert Employee after the insert Department
Sales emp3 = new Sales ();
id>
inverse table “is to abandon maintenance of association” (where two objects in java associate, the impact of database tables) in one-to-many and many-to-many collection used in the definition, inverse = “true” indicates that the object does not maintain the association; the property value is generally set when using an ordered set of false (Note that the hibernate default value is false)
depart.setEmps (emps);
Table I, the primary key for the id, subsidiary name, depart_id, type, skill
inheritance mapping Table one employee, the PK for the id, subsidiary name, depart_id
Session s = null;
tx.commit ();
emp3.setSell (100);
public class Sales extends Employee {
ields not limited to “not null”; must be added the type of this attribute
id>
emp2.setDepart (depart);
list>
->
*** in inverse = “true” role, will s.save (depart); to the front, followed by s.save (emp1 ); s.save (emp2); no update statement efficiency ~ ~ ~
s.save ( emp2);
we will give Teacher relationship maintenance, that is, when Teacher.hbm.xml the inverse = “true” time, t1 and t2 maintain relationships ignored, only to create an object of the association, s1 and s2 in the database have four insert statements
class>
insert into Employee (name, depart_id) values ??(?,?)
try {
s.save (depart);
New Skiller.java
mixed use, “a class hierarchy a table” and “table per subclass” (table structure)
——— ————— 31 about inheritance discriminator in connection with the combination of subclass – join table
set>
System.out.println (emp.getClass ());
}
in Many2One.java in
s1.setTeachers (ts);
< / subclass>
s = HibernateUtil.getSession ();
emp1.setName (“emp name1″);
->
tx.commit ();
xml version = "1.0"?>
join>
private String skill;
Cascade used to describe when the main whether a certain object when the object of their association also made from a similar operation, commonly used cascade: none, all, save-update, delete, lock, refresh, e vict, replicate, persist, merge, delete-orphan ( one-to-m any). Generally many-to-one, many-to-many cascade is not set, set in
s2.setTeachers (ts); / /? these four lines simultaneously, and why not?
modify Many2One.java
package cn.itcast.hibernate.domain;
subclass>
static Department add () {
Transaction tx = null;
subclass>
}
——————————- inherited about 29 relationship is mapped to the entire inheritance tree a table discriminator-value subclass
analysis Many2Many.java, which Teacher.hbm.xml the inverse default = false, the maintenance of relationships.
Department depart = new Department ();
Department.hbm.xml the following error code
dos command to drop / create database test; use test;
Note: insert into Employee (name, depart_id) values ??(?,?) in update_id is empty, so the need to update to update
emp2.setSkill (“skill”);
modify Employee . hbm.xml
}
one-to-many association is updated to maintain the foreign key. many-to-many association is to maintain the increase or decrease in the middle of the table records.
hibernate-mapping>
tx = s.beginTransaction ();
modify Employee.hbm.xml
a class hierarchy a table (subclass) (table structure) the employee object as the primary key PK id, attached with name, depart_id, type, skill, sell
emps.add (emp2);
subclass>
subclass>
if static Employee query (int empId) {Employee emp = (Employee) s.get (Employee.class, empId);}, as follows
select employee0_.id as id2_0_, employee0_.name as name2_0_, employee0_.depart_id as depart4_2_0, employee0_.skill as skill2_0_, employee0_.sell as sell2_0_, employee0_.type as type2_0_ from Employee0_ where employee0_.id =?
} finally {
hibernate-mapping>
Table III skiller, the PK, FK1 as employee_id, ancillary skill
static Employee query (int empId) {
New Sales.java