我有一个User类和员工类,其中员工类扩展和User类。另外,我有一个地址类,它是两个类的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>
构建时出现异常
线程中的异常&#34; main&#34; 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 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at 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个
答案 0 :(得分:0)
错误是由于缺少getter属性。
您应该将以下代码添加到地址类
public Set<Address> getAddresses() {
return addresses;
}
此外,请检查您是否还有setter属性。
private void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
Hibernate基本上需要需要持久保存的属性的getter和setter属性,如https://docs.jboss.org/hibernate/orm/5.0/mappingGuide/en-US/html/ch02.html#domainmodel-pojo-accessors所述。
您应该将setter设为私有,以便在更改期间强制执行类的数据封装。