我不是hibernate的常用用户。我正在尝试创建多对一映射,但我得到错误(subj)。我在寻找类声明中的错误,也是getter(最后一个错误),但一切似乎都是正确的。有没有人在我的代码中看到任何错误?因为我无法理解。
Adres.java
package beans;
public class Adres {
int id;
String adresas;
String adname;
public Adres() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAdresas() {
return adresas;
}
public void setAdresas(String adresas) {
this.adresas = adresas;
}
public String getAdname() {
return adname;
}
public void setAdname(String adname) {
this.adname = adname;
}
}
Men.java
package beans;
public class Men {
int id;
String name;
Adres adres;
public Men() {
}
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 Adres getAdd() {
return adres;
}
public void setAdd(Adres adres) {
this.adres = adres;
}
}
的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db2</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="hbm/Men.hbm.xml"/>
<mapping resource="hbm/Adres.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Men.hbm.xml
<hibernate-mapping>
<class name="beans.Men" table="MEN">
<id column="id" name="id" type="int">
<generator class="native"/>
</id>
<property column="name" name="name" type="string"/>
<many-to-one class="beans.Adres" column="adres" name="adres" not-null="true"/>
</class>
</hibernate-mapping>
Adres.bbm.xml
<hibernate-mapping>
<class name="beans.Adres" table="ADRES">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="adresas" column="adresas" type="string"/>
<property name="adname" column="adname" type="string"/>
</class>
</hibernate-mapping>
Main.java
private static SessionFactory factory;
public static void main(String[] args) {
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
错误日志
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
Exception in thread "main" java.lang.ExceptionInInitializerError
at main.HibernateUtil.<clinit>(HibernateUtil.java:15)
at main.Main.main(Main.java:17)
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:123)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:346)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at main.HibernateUtil.<clinit>(HibernateUtil.java:12)
... 1 more
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:91)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:116)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:388)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:508)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:124)
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:422)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
... 7 more
Caused by: 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:422)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:88)
... 16 more
Caused by: org.hibernate.PropertyNotFoundException: Could not locate getter method for property [beans.Men#adres]
at org.hibernate.internal.util.ReflectHelper.findGetterMethod(ReflectHelper.java:400)
at org.hibernate.property.access.internal.PropertyAccessBasicImpl.<init>(PropertyAccessBasicImpl.java:41)
at org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl.buildPropertyAccess(PropertyAccessStrategyBasicImpl.java:27)
at org.hibernate.mapping.Property.getGetter(Property.java:299)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:270)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:145)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:63)
... 21 more
答案 0 :(得分:0)
您应该从adres
类更改Men
属性的getter和setter
public Adres getAdd() {
return adres;
}
public void setAdd(Adres adres) {
this.adres = adres;
}
到
public Adres getAdres() {
return adres;
}
public void setAdres(Adres adres) {
this.adres = adres;
}
我建议提供有意义的变量名称和相关的getter,setter!