Java 无法获取 org.hibernate.persister.entity.SingleTableEntityPersister 错误的构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34022746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:14:52  来源:igfitidea点击:

Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister error

javaxmlspringhibernate

提问by Dainius Java

I am not a frequent user of hibernate. I am trying to create Many-to-one mapping, but I get error (subj). I was looking for a mistakes in class declarations, also getter (last error), but everything seems to be correct. Does anyone see any mistakes in my code? Because I am not able to figuew out.

我不是 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

男人.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

休眠文件.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

男人的.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);
        }
    }

Error log

错误日志

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

采纳答案by Omkar

You should change your getter and setter for adresproperty in Menclass from

您应该更改类中adres属性的getter 和 setterMen

public Adres getAdd() {
    return adres;
}

public void setAdd(Adres adres) {
    this.adres = adres;
}

to

public Adres getAdres() {
    return adres;
}

public void setAdres(Adres adres) {
    this.adres = adres;
}

I suggest to give a meaningfulvariable names and related getters, setters!

我建议给一个有意义的变量名和相关的getter、setter!