-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: org.hibernate.InstantiationException: No default constructor
PostPosted: Sun Mar 11, 2007 12:45 pm 
Newbie

Joined: Sun Mar 11, 2007 12:33 pm
Posts: 3
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 Testclass.main(Testclass.java:28)


But there IS the default constructor. Does anyone know what to do?

Thx in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 1:27 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, the message seems clear: no default constructor is accessible for ProjectPK.

And in fact:
Quote:
Code:
ProjectPK(){
         
      }


Add protected or public before this constructor declaration and you should be done. :)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 1:43 pm 
Newbie

Joined: Sun Mar 11, 2007 12:33 pm
Posts: 3
I tried your solution but it didn't change anything :(


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 11, 2007 3:23 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You put an internal non-static class. Are you sure that is what you want to do? At least, I would recommend you to set your internal class static.

In fact, you may want to use the ProjectPK without having a Project instance. For instance, you will need this to declare a foreign key referencing a project.

To sum up, I didn't verify, but I guess your error happens because your internal class is non-static.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 6:06 pm 
Newbie

Joined: Wed Nov 05, 2008 6:03 pm
Posts: 1
I had the same problem and it was gone when I changed the inner class to static.
I wonder if is there any workaround to have it working on non-static inner classes.

Anyway, thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 6:26 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
A non-static inner class never has a default no-argument constructor. It may look like that in the code, but when the class is compiled the Java compiler adds an implicit reference to an instance of the outer class. In the case of the first post the inner class actually has a constructor like this:

Code:
ProjectPK(Project project)
{
....
}


And since Hibernate requires a no-argument constructor it is not possible to use a non-static inner class.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.