此代码是:利用hibernate反向工程生成domain和domain的映射文件
基于主键的1-1映射
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
%%%% Error Creating SessionFactory %%%%
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:24)
at cn.itcast.hibernate.One2One.main(One2One.java:17)
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
at org.hibernate.cfg.Configuration.addResource(Configuration.java:588)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1606)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1574)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1553)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1527)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1447)
at cn.itcast.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at cn.itcast.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
at cn.itcast.hibernate.One2One.add(One2One.java:24)
at cn.itcast.hibernate.One2One.main(One2One.java:17)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping cn.itcast.hibernate.domain.Person
at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:145)
at org.hibernate.cfg.Configuration.add(Configuration.java:688)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:523)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:585)
... 9 more
Exception in thread "main" java.lang.NullPointerException
at cn.itcast.hibernate.One2One.add(One2One.java:34)
at cn.itcast.hibernate.One2One.main(One2One.java:17)
IdCard类属性
private Integer id;
private Person person;
private Date usefulLife;
Person类属性
private Integer id;
private String name;
private IdCard idCard;
由hibernate反向工程生成的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="19" not-null="true" />
</property>
</class>
</hibernate-mapping>
以上存在错误,手动改成以下形式
<id name="id" type="java.lang.Integer">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
望君牢记