inverse为true时表示将主控权交给关联类来执行,在为false时表示由自己来行驶主控权。
inverse属性默认为false,也就是说关系的两端都维护关系。
在一对多映射中,inverse属性多设置为true,也就是说,一对多映射的映射关
系最好由多的一方来维护。
如下所示,为建立的Boy与Girl的关系一对多模型,一个Boy对应多个Girl。
Boy.java
package hibernate.persistance;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Boy {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
private byte[] photo;
private Date date;
private String description;
private Set<Girl> girls=new HashSet<Girl>();
public Set<Girl> getGirls() {
return girls;
}
public void setGirls(Set<Girl> girls) {
this.girls = girls;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Boy.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.persistance">
<class name="Boy" table="boy">
<id name="id" column="id" type="integer">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string"></property>
<property name="photo" column="photo" type="binary"></property>
<property name="date" type="date">
<column name="date"></column>
</property>
<property name="description" column="description" type="string"></property>
<set name="girls" lazy="true" cascade="all" inverse="true">
<key column="bid"></key>
<one-to-many class="Girl"/>
</set>
</class>
</hibernate-mapping>
Girl.java
package hibernate.persistance;
import java.sql.Date;
public class Girl {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
private byte[] photo;
private Date date;
private String description;
private Boy boy;
public Boy getBoy() {
return boy;
}
public void setBoy(Boy boy) {
this.boy = boy;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Girl.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.persistance">
<class name="Girl" table="girl">
<id name="id" column="id" type="integer">
<generator class="native"></generator>
</id>
<property name="name" column="name" type="string"></property>
<property name="photo" column="photo" type="binary"></property>
<property name="date" column="date" type="date"></property>
<property name="description" column="description" type="string"></property>
<many-to-one name="boy" column="bid" class="Boy" cascade="all"></many-to-one>
</class>
</hibernate-mapping>
测试代码:
public static void test1(){
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
//Boy b=new Boy();
Boy boy = new Boy();
Set girls = new HashSet();
Girl g[] = new Girl[]{
new Girl(),
new Girl(),
new Girl()
};
girls.add(g[0]);
girls.add(g[1]);
girls.add(g[2]);
g[0].setBoy(boy);
g[1].setBoy(boy);
g[2].setBoy(boy);
s.save(g[0]);
s.save(g[1]);
s.save(g[2]);
tx.commit();
s.close();
}
Hibernate只执行了插入语句:
Hibernate: insert into boy (name, photo, date, description) values (?, ?, ?, ?)
Hibernate: insert into girl (name, photo, date, description, bid) values (?, ?, ?, ?, ?)
Hibernate: insert into girl (name, photo, date, description, bid) values (?, ?, ?, ?, ?)
Hibernate: insert into girl (name, photo, date, description, bid) values (?, ?, ?, ?, ?)
防止重复sql语句的生成,因此,inverse设为true的用处就体现出来了。
而如果invers=false,则会生成重复sql语句,如下:
public static void test1(){
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
//Boy b=new Boy();
Boy boy = new Boy();
Set girls = new HashSet();
Girl g[] = new Girl[]{
new Girl(),
new Girl(),
new Girl()
};
girls.add(g[0]);
girls.add(g[1]);
girls.add(g[2]);
boy.setGirls(girls);
s.save(boy);
tx.commit();
s.close();
}
Hibernate除了执行插入语句,还执行了update语句
Hibernate: insert into boy (name, photo, date, description) values (?, ?, ?, ?)
Hibernate: insert into girl (name, photo, date, description, bid) values (?, ?, ?, ?, ?)
Hibernate: insert into girl (name, photo, date, description, bid) values (?, ?, ?, ?, ?)
Hibernate: insert into girl (name, photo, date, description, bid) values (?, ?, ?, ?, ?)
Hibernate: update girl set bid=? where id=?
Hibernate: update girl set bid=? where id=?
Hibernate: update girl set bid=? where id=?