1 在类当中的属性是用匈牙利命名法做的,但是在数据库当中使用的命名法是小写加上下划线。所以这个时候应该尽肯能的兼顾两个方面
2 org.hibernate.MappingException: Unknown entity: cn.itcast.domain.Person
这个一般都是因为没有在hibernate.cfg.xml中配置实体文件。
3 attempted to assign id from null one-to-one property [cn.itcast.domain.IdCard.person]
也就是说,IdCard.person这个属性没有设置
4 也就是说,如果有一个类,他有两个属性,一个是id,另一个是anotherClass,先创建anotherClass,设置完成他的属性以后,就回到原来的类setAnotherClass()
5 Exception in thread "main" java.lang.ExceptionInInitializerError
at cn.itcast.dao.test.one2one.add(one2one.java:29)
at cn.itcast.dao.test.one2one.main(one2one.java:20)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:108)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:133)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:322)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at cn.itcast.dao.HibernateUtil.<clinit>(HibernateUtil.java:29)
... 2 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:105)
... 11 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for usefulLife in class cn.itcast.domain.IdCard
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:326)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:320)
at org.hibernate.mapping.Property.getGetter(Property.java:330)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:297)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:155)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)
... 16 more
今天看到这个异常,我的方法是一直看下面的异常,这样的话我们就可以找到原因了。
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for usefulLife in class cn.itcast.domain.IdCard
也就是说,没有get方法usefulLife
6 在一对一映射当中加上这个constraint属性的话,是映射主键与外键
Person.java
package cn.itcast.domain;
public class Person {
private int id;
private String name;
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 IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.domain">
<class name="Person">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<one-to-one name="idCard"></one-to-one>
</class>
</hibernate-mapping>
IdCard.java
package cn.itcast.domain;
import java.util.Date;
public class IdCard {
private int id;
private Date usefulLife;
public Date getUsefulLife() {
return usefulLife;
}
public void setUsefulLife(Date usefulLife) {
this.usefulLife = usefulLife;
}
private Person person;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
IdCard.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.domain">
<!-- class指明类的名称,在person先看下面,让后在看上面的person
foreign表明我的id不是自己生产的,而是从外面拿过来的,这样保证
与person.id一致
-->
<class name="IdCard" table="id_card">
<id name="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="usefulLife" column="useful_life"></property>
<one-to-one name="person" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
one2one.java
package cn.itcast.dao.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.logicalcobwebs.asm.tree.TryCatchBlockNode;
import cn.itcast.dao.HibernateUtil;
import cn.itcast.domain.IdCard;
import cn.itcast.domain.Person;
public class one2one {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
add();
}
static Person add()
{
Session s=null;
Transaction tx=null;
try {
s=HibernateUtil.getSession();
IdCard idCard=new IdCard();
idCard.setUsefulLife(new Date());
Person person=new Person();
person.setName("person1");
person.setIdCard(idCard);
idCard.setPerson(person);
tx=s.beginTransaction();
s.save(person);
s.save(idCard);
tx.commit();
return person;
} finally {
if(s!=null)
s.close();
}
}
}