【发布时间】:2018-05-30 10:28:28
【问题描述】:
我有一个用户类和员工类,其中员工类扩展和用户类。另外,我有一个地址类,它与这两个类都有一个 HAS-A 关系。下面是实现类和错误。我收到错误消息。
用户模型
public class User {
private int userid;
private String username;
private String password;
private Date dob;
private String gender;
private Set<Address> addresses;
//getter and setters
}
员工模型
public class Employee extends User {
private Date joindate;
private String role;
private Branch branch;
//getter and setters
}
地址模型
public class Address {
private String email;
private String mobile;
private String street;
private String city;
private String state;
private String country;
private String pin;
//getter & setters
}
users.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.alok.mypro.model.usermodel.User" table="myuser" schema="mydb">
<id name="userid">
<generator class="increment" />
</id>
<property name="username" />
<property name="password" />
<property name="dob" />
<property name="gender" />
<set name="addresses" cascade="all">
<key column="userid" />
<one-to-many class="com.alok.mypro.model.usermodel.Address" />
</set>
<joined-subclass name="com.alok.mypro.model.usermodel.Employee" table="employee">
<key column="userid" />
<property name="joindate" />
<property name="role" />
<many-to-one name="branch" class="com.alok.mypro.model.places.Branch"/>
</joined-subclass>
</class>
</hibernate-mapping>
address.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.alok.mypro.model.usermodel.Address" table="address"
schema="myuser">
<id name="email" />
<property name="mobile" />
<property name="street" />
<property name="city" />
<property name="state" />
<property name="country" />
<property name="pin" />
</class>
</hibernate-mapping>
构建时出现异常
线程“主”org.hibernate.HibernateException 中的异常:无法实例化默认 tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer] 在 org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:108) 在 org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:133) 在 org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:80) 在 org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:322) 在 org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:485) 在 org.hibernate.persister.entity.JoinedSubclassEntityPersister.(JoinedSubclassEntityPersister.java:126) 在 org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:87) 在 org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:286) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872) 在 Test.main(Test.java:7)
引起:java.lang.reflect.InvocationTargetException 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在 org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:105)
原因:org.hibernate.PropertyNotFoundException:在 com.alok.mypro.model.usermodel.Employee 类中找不到地址的 getter 在 org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:326) 在 org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:320) 在 org.hibernate.mapping.Property.getGetter(Property.java:304) 在 org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:297) 在 org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:155) 在 org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:77) ... 14 更多
【问题讨论】:
标签: java xml hibernate hibernate-mapping