hibernate映射---多对一(list)
在学校宿舍里面,床和宿舍(tb_bed\tb_room)就是多对一的关系。
cascade的属性:
级联指的是当主控方执行操作时,关联对象(被动方)是否同步执行同一操作。pojo和它的关系属性的关系就是“主控方 -- 被动方”的关系,如果关系属性是一个list,那么被动方就是list中的一个一个元素。
比如删除tb_room----session.delete(room);级联决定了是否执行关联到tb_room的tb_bed---session.delete(bed);
cascade="save-update": 级联保存(load以后如果子对象发生了更新,也会级联更新), 但它不会级联删除。一对一、一对多关系(首端为一)一般设置为save-update;
cascade="delete": 级联删除, 但不具备级联保存和更新(例如删除班级,将级联删除下属的学生);
all-delete-orphan: 在解除父子关系时,自动删除不属于父对象的子对象, 也支持级联删除和级联保存更新。主要用于从集合中删除对象的情况,此时被删除的对象将会被删除成为脱管对象。
all: 级联删除, 级联更新,但解除父子关系时不会自动删除子对象.;
none:多对一、多对多关系(首端为多)一般设置为none。
(1)表结构:如图片所示,tb_bed通过room_sn关联到tb_room中。
create table TB_BED
(
SN NUMBER not null,
ROOM_SN NUMBER,
NAME VARCHAR2(20),
USERNAME VARCHAR2(20),
BEDINDEX NUMBER
)
create table TB_ROOM
(
SN NUMBER,
NAME VARCHAR2(20),
ADDRESS VARCHAR2(50),
FLOOR VARCHAR2(2)
)
(2)映射文件
tb_room的映射文件如下:
<hibernate-mapping>
<class name="com.longtop.entity.TbRoom" table="TB_ROOM" schema="TEST">
<id name="sn" type="java.lang.Long" column="SN">
<generator >
<param name="sequence">seq_tb_room_sn</param>
</generator>
</id>
<property name="floor" type="string">
<column name="FLOOR" length="2"/>
</property>
<property name="name" type="string">
<column name="NAME" length="20" />
</property>
<property name="address" type="string">
<column name="ADDRESS" length="50" />
</property>
<list name="beds" table="TB_ROOM" lazy="false" inverse="true" cascade="all-delete-orphan" >
<key column="ROOM_SN" />
<index column="BEDINDEX"/>
<one-to-many table="TB_BED" schema="TEST">
<id name="sn" type="java.lang.Long" column="SN">
<generator >
<param name="sequence">seq_tb_bed_sn</param>
</generator>
</id>
<property name="roomSn" type="long">
<column name="ROOM_SN" length="22"/>
</property>
<property name="name" type="string">
<column name="NAME" length="20" />
</property>
<property name="username" type="string">
<column name="USERNAME" length="20" />
</property>
<property name="bedindex" type="long">
<column name="BEDINDEX" length="20" />
</property>
</class>
</hibernate-mapping>
(3)tb_room、tb_bed的映射类如下:
public class TbRoom {
private Long sn;
private String name;
private String address;
private String floor;
private List beds = new ArrayList();
public List getBeds() {
return beds;
}
public void setBeds(List beds) {
this.beds = beds;
}
public Long getSn() {
return sn;
}
public void setSn(Long sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFloor() {
return floor;
}
public void setFloor(String floor) {
this.floor = floor;
}
}
tb_bed的映射类代码如下:
public class TbBed {
private Long sn;
private Long roomSn;
private String username;
private String name;
private Long bedindex;
public Long getBedindex() {
return bedindex;
}
public void setBedindex(Long bedindex) {
this.bedindex = bedindex;
}
public Long getSn() {
return sn;
}
public void setSn(Long sn) {
this.sn = sn;
}
public Long getRoomSn() {
return roomSn;
}
public void setRoomSn(Long roomSn) {
this.roomSn = roomSn;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(3)测试类
(一)测试类的测试代码
public class testMain {
public static void main(String[] args) throws HibernateException {
SessionFactory sessions =
new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
Long start = new Date().getTime();
System.out.println("starttime:"+start);
// Long sn = testMain.createRoom(session);
TbRoom room = testMain.loadRoom(session, new Long(23));
testMain.deleteBed(session, null, room);
tx.commit();
session.close();
Long end = new Date().getTime();
System.out.println("endtime:"+end);
System.out.println("总共用了"+(end-start)+"的时间");
}
}
(二)添加记录的方法(添加tb_room记录并添加对应到该tb_room的tb_bed对象)。
private static Long createRoom(Session session){
TbRoom room = new TbRoom();
room.setName("102");
room.setFloor("1");
room.setAddress("公寓1号楼");
TbBed bed1 = new TbBed();
bed1.setName("1床");
bed1.setUsername("aimy");
TbBed bed2 = new TbBed();
bed2.setName("2床");
bed2.setUsername("Lucy");
TbBed bed3 = new TbBed();
bed3.setName("3床");
bed3.setUsername("judy");
TbBed bed4 = new TbBed();
bed4.setName("4床");
bed4.setUsername("rose");
try {
room.getBeds().add(bed1);
room.getBeds().add(bed2);
room.getBeds().add(bed3);
room.getBeds().add(bed4);
session.save(room);//只要保存一次tb_room,则自动保存对应的tb_bed
// session.save(bed1);
// session.save(bed2);
// session.save(bed3);
// session.save(bed4);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return room.getSn();
}
}
在添加对象时,出现了以下的错误及情况:
(1)Exception in thread "main" java.lang.NullPointerException
at testMain.createRoom(testMain.java:102)
at testMain.main(testMain.java:30)
原因:testMain.java:102就是 room.getBeds().add(bed1); 行。room.getBeds()为null,所以没办法调用add方法,
解决的方法:必须在com.longtop.entity.TbRoom映射类中,声明tb_bed集合List beds时初始化。 private List beds = new ArrayList();
(2)如果tb_room的映射文件中list子元素如下设置:
<list name="beds" table="TB_ROOM" lazy="false" inverse="true" cascade="all-delete-orphan" >
<key column="ROOM_SN" />
<index column="BEDINDEX"/>
<one-to-many table="TB_ROOM" lazy="false" inverse="false" cascade="all-delete-orphan" >
<key column="ROOM_SN" />
<index column="BEDINDEX"/>
<one-to-many table="TB_ROOM" lazy="false"
inverse="true" cascade="all-delete-orphan" >
<key column="ROOM_SN" />
<index column="BEDINDEX"/>
<one-to-many table="TB_ROOM" lazy="false"
inverse="false" cascade="all-delete-orphan" >
<key column="ROOM_SN" />
<index column="BEDINDEX"/>
<one-to-many class="com.longtop.entity.TbBed"/>
</list>
③删除tb_room的某一条tb_bed记录。
private static void deleteBed(Session session,TbBed bed,TbRoom room)
throws HibernateException{
System.out.println("删除room床号0床位!");
room.getBeds().remove(0);
session.save(room);
}