使用连接表的单向关联【一对多(one-to-many)】
基于连接表的单向一对多关联 应该优先被采用。请注意,通过指定unique="true",我们可以把多样性从多对多改变为一对多。
?
?
person.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> <class name="com.lixueli.business.model.Person" table="person"> <id name="id" column="PERSON_ID"> <generator table="PERSON_EVENT" lazy="false"> <key column="PERSON_ID"></key> <many-to-many column="EVENT_ID" unique="true"></many-to-many> </set> </class> </hibernate-mapping>
?Event.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> <class name="com.lixueli.business.model.Event" table="event"> <id name="id" column="EVENT_ID"> <generator /> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title" /> </class> </hibernate-mapping>
?这样便实现单向一对多关联:一个person可以对应多个Event,但一个Event只能对应一个Person.
?