日期:2014-05-19  浏览次数:20650 次

Hibernate实体为什么要提供一个无参的构造函数
@Entity
@Table(name="user")
public class Person {

private int ID;
private String name;
private String cellphone;
/*
* 为什么需要一个默认的构造函数
*/
/*public Person() {
}*/

public Person(String name, String cellphone) {
this.name = name;
this.cellphone = cellphone;
}

@Id
public int getID() {
return ID;
}

public void setID(int iD) {
ID = iD;
}

@Column(name = "NAME")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Column(name = "CELLPHONE")
public String getCellphone() {
return cellphone;
}

public void setCellphone(String cellphone) {
this.cellphone = cellphone;
}

}
如上所示把无参构造函数注释调以后,运行测试用例
@Test
public void testUpdate() {
Person person = personService.getPerson(1);
//....
person.setName("wang");
personService.update(person);
}
报如下异常:
org.hibernate.InstantiationException: No default constructor for entity: com.gosophia.entity.Person
 at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:107)
 at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:123)
 at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:374)
 at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3634)
 at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1302)
 at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1291)
 at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1323)
 at org.hibernate.loader.Loader.getRow(Loader.java:1230)
 at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
 at org.hibernate.loader.Loader.doQuery(Loader.java:724)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
 at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
 at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
 at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
 at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
 at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
 at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
 at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
 at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
 at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
 at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
 at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
 at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
 at com.gosophia.service.impl.PersonServiceBean.getPerson(PersonServiceBean.java:27)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
&