Hallo!
Ich habe ein (kleines) Problemchen. Und zwar bekomme ich Exceptions von Hibernate - Could not find a getter for recht in class main.java.org.hibernate.DB.User
Folgendes Szenario:
Ein User hat genau ein Recht. Ein Recht ist mehreren Usern zugeteilt.
Das ist so in der Datenbank: (HSQL)
Code
CREATE TABLE Rechte
(
r_id INT GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,
rechteBEZ VARCHAR(50)
);
CREATE TABLE User
(
u_id INT GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,
RechteID INTEGER,
username VARCHAR(50),
passwort VARCHAR(50),
nachname VARCHAR(50),
vorname VARCHAR(50),
email VARCHAR(50),
geburtsdatum VARCHAR(10),
geschlecht CHAR(1),
avatar VARCHAR(100),
adresse VARCHAR(50),
plz INTEGER,
ort VARCHAR(50),
telefon VARCHAR(20),
frage VARCHAR(100),
antwort VARCHAR(50),
kamera VARCHAR(50),
vorlieben VARCHAR(200),
motive VARCHAR(200),
FOREIGN KEY(RechteID) REFERENCES Rechte(R_ID)
);
Alles anzeigen
dann habe ich die xml files für hibernate erstellt nach tutorial ( link1 und link2
User.hbm.xml
XML
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="main.java.org.hibernate.DB">
<class name="User" table="User">
<id name="id" type="int" column="u_id">
<generator class="native" />
</id>
<property name="username" />
<property name="passwort" />
<property name="nachname" />
<property name="vorname" />
<property name="email" />
<property name="geburtsdatum" />
<property name="geschlecht" />
<property name="avatar" />
<property name="adresse" />
<property name="plz" />
<property name="ort" />
<property name="telefon" />
<property name="frage" />
<property name="antwort" />
<property name="vorlieben" />
<property name="kamera" />
<property name="motive" />
<many-to-one name="rechte" class="Rechte" column="RechteID" not-null="true" />
</class>
</hibernate-mapping>
Alles anzeigen
Rechte.hbm.xml
XML
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="main.java.org.hibernate.DB">
<class name="Rechte" table="Rechte">
<id name="id" type="int" column="r_id">
<generator class="native"/>
</id>
<property name="rechteBEZ"/>
<set name="users" inverse="true">
<key column="RechteID" not-null="true" />
<one-to-many class="User"/>
</set>
</class>
</hibernate-mapping>
Alles anzeigen
Dann die Hibernate Sessions:
Code
public void save(User user) throws RuntimeException
{
try
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.saveOrUpdate(user);
session.getTransaction().commit();
}
catch (Exception e)
{
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
throw new RuntimeException(e.getMessage());
}
finally
{
// HibernateSessionFactory.closeSession();
}
public List<?> getAll() throws RuntimeException
{
List list = null;
try {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
list = session.createQuery("from " + User.class.getName()).list();
session.getTransaction().commit();
} catch (HibernateException e) {
// throw new RuntimeException(e.getMessage());
throw e;
} finally {
// HibernateSessionFactory.closeSession();
}
return list;
}
Alles anzeigen
und zu guter letzt meine Testimplementierungen.
Code
private static void testRechte()
{
r = new RechteHibernate();
Rechte rechte = new Rechte();
List recht = r.getAll();
for (int i = 0; i < recht.size(); i++)
{
Rechte re = (Rechte) recht.get(i);
if (re.getRechteBEZ().equals("Admin"))
{
rechte = re;
}
}
User u = new User();
u.setRechte(rechte);
rechte.getUsers().add(u);
UserHibernate uh = new UserHibernate();
uh.save(u);
}
Alles anzeigen
Und meine testimplementierung für User aus der Datenbank auslesen:
Code
private static void listUsers()
{
List users = u.getAll();
for (int i = 0; i < users.size(); i++)
{
User user = (User) users.get(i);
System.out.println("Username: " + user.getUsername());
System.out.println(user.getRechte());
}
}
Alles anzeigen
Exception:
Code
Initial SessionFactory creation failed.org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
Exception in thread "main" java.lang.ExceptionInInitializerError
at main.java.org.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at main.java.org.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
at Hibernate.RechteHibernate.getAll(RechteHibernate.java:108)
at Tests.TestRecht.listRechte(TestRecht.java:79)
at Tests.TestRecht.main(TestRecht.java:22)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:110)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:456)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at main.java.org.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 4 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:107)
... 13 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for recht in class main.java.org.hibernate.DB.User
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:306)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:299)
at org.hibernate.mapping.Property.getGetter(Property.java:294)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:300)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:141)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:78)
... 18 more
Alles anzeigen
meine Klasse User.java:
Code
public class User implements Serializable
{
private static final long serialVersionUID = -6829822767175518387L;
private String username;
private String passwort;
private String nachname;
private String vorname;
private String email;
private String geburtsdatum;
private char geschlecht;
private String avatar;
private String adresse;
private int plz;
private String ort;
private String telefon;
private String frage;
private String antwort;
private String kamera;
private String vorlieben;
private String motive;
private int u_id;
private Rechte rechte;
public Rechte getRechte()
{
return rechte;
}
public void setRechte(Rechte rechte)
{
this.rechte = rechte;
}
.........
Alles anzeigen
meine Rechte.java
Java
package main.java.org.hibernate.DB;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Rechte implements Serializable
{
private static final long serialVersionUID = 3798844407433161889L;
private String rechteBEZ;
private int r_id;
private Set users = null;
public Rechte()
{
users = new HashSet();
}
public Set getUsers()
{
return users;
}
public void setUsers(Set users)
{
this.users = users;
}
public int getId()
{
return r_id;
}
public void setId(int r_id)
{
this.r_id = r_id;
}
public String getRechteBEZ()
{
return rechteBEZ;
}
public void setRechteBEZ(String rechteBEZ)
{
this.rechteBEZ = rechteBEZ;
}
public String toString()
{
return this.rechteBEZ + " " + this.r_id;
}
}
Alles anzeigen
Ich weiß ned woher die :cuss: das "recht" herhat
Vielleicht kann jemand helfen?
edit: zur übersicht hab ich noch alle files, die in frage kommen, hier: