1対1のプライマリ・キー・マッピング関係

6532 ワード

この例では、公民と身分証明書番号は一対一の関係です.
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)