Hi there
I have two classes: Project and User
Project
Code:
package de.peguform.projectmanager.project.hibernate;
import java.io.Serializable;
public class Project {
public class ProjectPK implements Serializable{
ProjectPK(){
}
private String name;
private int createdate;
public int getCreatedate() {
return createdate;
}
public void setCreatedate(int createdate) {
this.createdate = createdate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private String name;
private int createdate;
private String description;
private String creator;
private ProjectPK projectPK;
public Project(){
projectPK = new ProjectPK();
}
public int getCreatedate() {
return createdate;
}
public void setCreatedate(int createdate) {
this.createdate = createdate;
projectPK.setCreatedate(createdate);
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
projectPK.setName(name);
}
public ProjectPK getProjectPK() {
return projectPK;
}
public void setProjectPK(ProjectPK projectPK) {
this.projectPK = projectPK;
}
}
Mapping
Code:
<?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>
<class name="de.peguform.projectmanager.project.hibernate.Project" table="Project">
<composite-id name="projectPK" class="de.peguform.projectmanager.project.hibernate.Project$ProjectPK">
<key-property name="name" column="name"/>
<key-property name="createdate" column="createdate"/>
</composite-id>
<property name="description">
<column name="description"/>
</property>
<property name="creator">
<column name="creator"/>
</property>
</class>
</hibernate-mapping>
User
Code:
package de.peguform.projectmanager.user.hibernate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class User {
private String username;
private String firstname;
private String lastname;
private String role;
private String email;
private Set projects = new HashSet() ;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Set getProjects() {
return projects;
}
public void setProjects(Set projects) {
this.projects = projects;
}
}
Mapping
Code:
<?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>
<class name="de.peguform.projectmanager.user.hibernate.User"
table="User">
<id name="username" column="username" />
<property name="lastname">
<column name="lastname" />
</property>
<property name="firstname">
<column name="firstname" />
</property>
<property name="role">
<column name="role" />
</property>
<property name="email">
<column name="email" />
</property>
<set name="projects">
<key column="creator"/>
<one-to-many class="de.peguform.projectmanager.project.hibernate.Project" />
</set>
</class>
</hibernate-mapping>
If i call the create()- Method of my ProjectCRUD class or perform an update or read everything works fine. But since i defined the one-to-many mapping and try to get a user (with all projects created by him) hibernate doesn't seem to be able to initialize the inner class which i use for the primary key of Project.
All i get is:
Code:
Hibernate: select this_.username as username0_0_, this_.lastname as lastname0_0_, this_.firstname as firstname0_0_, this_.role as role0_0_, this_.email as email0_0_ from User this_
userBenjaZ
Hibernate: select projects0_.creator as creator1_, projects0_.name as name1_, projects0_.createdate as createdate1_, projects0_.name as name1_0_, projects0_.createdate as createdate1_0_, projects0_.description as descript3_1_0_, projects0_.creator as creator1_0_ from Project projects0_ where projects0_.creator=?
Exception in thread "main" org.hibernate.InstantiationException: No default constructor for entity: de.peguform.projectmanager.project.hibernate.Project$ProjectPK
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:84)
at org.hibernate.tuple.component.AbstractComponentTuplizer.instantiate(AbstractComponentTuplizer.java:89)
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:482)
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:488)
at org.hibernate.type.ComponentType.resolve(ComponentType.java:580)
at org.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:275)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1088)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:553)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1985)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
at PimmelKlasseEins.main(Testclass.java:28)
But there IS the default constructor. Does anyone know what to do?
Thx in advance