user2258338 user2258338 - 2 months ago 12
Java Question

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

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.


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);
}
}



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

Answer

You should change your getter and setter for adres property in Men class from

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;
}