两个类Person、IdCard
Person:主键id
private Integer id;
private String name;
private IdCard idCard;
IdCard:主键id 外键id (此处的id即为Person里面的id)
private Integer id;
private Person person;
private Date usefulLife;
出现错误:
org.hibernate.MappingException: could not instantiate id generator [entity-name=cn.itcast.hibernate.domain.IdCard]
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:109)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:197)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1313)
at cn.itcast.hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
at cn.itcast.hibernate.One2One.add(One2One.java:25)
at cn.itcast.hibernate.One2One.main(One2One.java:18)
Caused by: org.hibernate.MappingException: param named "property" is required for foreign id generation strategy
at org.hibernate.id.ForeignGenerator.configure(ForeignGenerator.java:82)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:104)
... 6 more
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource cn/itcast/hibernate/domain/Person.hbm.xml
解决方法:
myeclipse10自动生成IdCard.hbm.xml文件内容如下
<hibernate-mapping>
<class name="cn.itcast.hibernate.domain.IdCard" table="id_card" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="foreign"></generator>
</id>
<one-to-one name="person" class="cn.itcast.hibernate.domain.Person" constrained="true"></one-to-one>
<property name="usefulLife" type="java.util.Date">
<column name="useful_life" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>
应该改为:
<id name="id" type="java.lang.Integer">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>