one-to-one关联(唯一外键关联)
唯一外键一对一关联是多对一关联的一种特例。在子表中,相同的外键只能出现一次,从而保证一对一
本例的关键在于many-to-one与one-to-one中property-ref这个属性含义的了解
1.实体类
这里以顾客与房间为例(一个顾客只能住一个房间):
Customer类:
public class Customer {private int customerId;private String customerName;private Room room;public Room getRoom() {return room;}public void setRoom(Room room) {this.room = room;}public int getCustomerId() {return customerId;}public void setCustomerId(int customerId) {this.customerId = customerId;}public String getCustomerName() {return customerName;}public void setCustomerName(String customerName) {this.customerName = customerName;}}
?Room:
public class Room {private int roomId;private String roomName;private Customer customer;public int getRoomId() {return roomId;}public void setRoomId(int roomId) {this.roomId = roomId;}public String getRoomName() {return roomName;}public void setRoomName(String roomName) {this.roomName = roomName;}public Customer getCustomer() {return customer;}public void setCustomer(Customer customer) {this.customer = customer;}}
?2.映射文件
customer.hbm.xml
<hibernate-mapping package="org.zengge.hibernate.onetoone.pojo"><class name="Customer" table="customer"><id name="customerId" type="integer"><generator /></id><property name="customerName" type="string"/><many-to-one name="room" column="roomId" unique="true"/><!-- property-ref: (可选) 指定关联类的一个属性,这个属性将会和本外键相对应。 如果没有指定,会使用对方关联类的主键。 也就是说默认Customer的外键与Room的主键相关联,如果外键要与Room的其它属性相关联就必须指定一个Room类中的属性。 --></class></hibernate-mapping>
?对于many-to-one:
name:与外键相关联的属性名
class:相关联的类名
column:many-to-one会在子表中新建一个外键列,用于指定外键名
unique:表示为该列添加唯一性约束
?
Room.hbm.xml
<hibernate-mapping package="org.zengge.hibernate.onetoone.pojo"><class name="Room" table="room"><id name="roomId" type="integer"><generator type="string" ></property><one-to-one name="customer" property-ref="room"></one-to-one><!-- 这里的property-ref=room指定的是Customer类中与Room关联的属性,而不是表中的,所以不是roomIdproperty-ref: (可选) 指定关联类的属性名,这个属性将会和本类的主键相对应。如果没有指定,会使用对方关联类的主键。在Customer类中room对应表中的roomId,那么就是说用Customer中的外键roomId与Room中的主键roomId相关联。如果没指定,就会将roomId与customerId关联,这样显然是不对的 --></class></hibernate-mapping>
?one-to-one:
name表示相关联的属性名
class表示相关联的类名
property-ref(上面己讲),这个是关键,切记它指定的是相关联的类中的属性,而不是表中的列名
?
3.hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="org/zengge/hibernate/onetoone/pojo/Customer.hbm.xml"/> <mapping resource="org/zengge/hibernate/onetoone/pojo/Room.hbm.xml"/> </session-factory></hibernate-configuration>
?4.测试
public void testSave(){Session session = HibernateUtils.getSession();Transaction tr = session.beginTransaction();Room room = new Room();room.setRoomName("moon");Customer customer = new Customer();customer.setCustomerName("sky");session.save(room);customer.setRoom(room);session.save(customer);tr.commit();}