一对一主键映射关系

这个例子中以:公民与身份证号是一对一的关系

public class TabPeople {
	
	private int id;
	private String name;
	private int sex;
	private int age;
	private IDCard idCard;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public IDCard getIdCard() {
		return idCard;
	}
	public void setIdCard(IDCard idCard) {
		this.idCard = idCard;
	}
}	

 

<hibernate-mapping>
	<!-- 每一个class类对应一个持久化对象 -->
	<class name="model.TabPeople" table="mw_peo">
		<id name="id" column="id" type="int">
		<!-- 主键的生成策略 -->
			<generator class="native"/>
		</id>
		<property name="name"></property>
		<property name="age"></property>
		<property name="sex"></property>
		<one-to-one name="idCard" class="model.IDCard" cascade="all"></one-to-one>
	</class>
</hibernate-mapping>

 

public class IDCard {
	
	private int id;
	private int cardCode;
	private TabPeople people;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getCardCode() {
		return cardCode;
	}
	public void setCardCode(int cardCode) {
		this.cardCode = cardCode;
	}
	public TabPeople getPeople() {
		return people;
	}
	public void setPeople(TabPeople people) {
		this.people = people;
	}
	
	
}

 

<hibernate-mapping>
	<!-- 每一个class类对应一个持久化对象 -->
	<class name="model.IDCard" table="mw_card">
		<id name="id" column="id" type="int">
		<!-- 主键的生成策略 -->
			<generator class="foreign">
				<param name="property">people</param>
			</generator>
		</id>
		<property name="cardCode"></property>
		<one-to-one name="people" class="model.TabPeople" constrained="true"/>
	</class>
</hibernate-mapping>

 

	public static void main(String[] args) {
		Configuration configuration=new Configuration().configure();
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		TabPeople people = new TabPeople();
		people.setAge(18);
		people.setName("zhansan");
		people.setSex(1);
		IDCard card=new IDCard();
		card.setCardCode(7238498);
		people.setIdCard(card);
		card.setPeople(people);
		session.save(people);
		
		transaction.commit();
		session.close();
	}

 注意测试类中一定要写card.setPeople(people),因为IDCard中的主键是根据people中的主键来获取,如果去掉这句代码,会出现以下错误:

Exception in thread "main" org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [model.IDCard.people]
	at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:101)
	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
	at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:615)
	at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:608)
	at org.hibernate.engine.spi.CascadingAction$5.cascade(CascadingAction.java:258)
	at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
	at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
	at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
	at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
	at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:462)
	at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:337)
	at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)
	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:128)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
	at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
	at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
	at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:642)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:635)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:631)
	at test.TestApplication8.main(TestApplication8.java:36)

 

你可能感兴趣的:(one-to-one)