-->
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:44 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 PimmelKlasseEins.main(Testclass.java:28)


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

Thx in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 10:29 am 
Newbie

Joined: Fri Jun 02, 2006 9:31 pm
Posts: 7
This one bit me too.
You need to pay close attention to the declaration of your inner class.

Should be:

public static class ProjectPK implements Serializable

_________________
"Man's Ego is the fountainhead of Human progress..."
(Ayn Rand)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 1:31 pm 
Newbie

Joined: Tue Apr 17, 2007 9:02 am
Posts: 7
Could you please explain the reason for making it static?

Thanks.

_________________
Excellence Inc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 1:51 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Only static inner classes can be instantiated outside of their enclosing class. Hibernate needs to instantiate the PK class when constructing the Project. Good catch jaysingh, give that man some credit!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 2:09 pm 
Newbie

Joined: Tue Apr 17, 2007 9:02 am
Posts: 7
Thanks Ananasi, your explanation is as helpful as equality72521's response..

Not sure how the credit thing works but i think only the thread initiator can give credit :)

_________________
Excellence Inc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 2:18 pm 
Newbie

Joined: Fri Jun 02, 2006 9:31 pm
Posts: 7
Check out this section of the Java language spec that explains the difference between static and non-static inner classes:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3

A static inner class is conceptually no different than a regular general class declared in a .java file.

Since Hibernate needs to instantiate ProjectPK independantly of the Project instance, ProjectPK either needs to be a static inner class, or declared in it's own .java file.

Here's a quick example that illustrates the difference between static and non-static inner classes:

Code:
public class Project {
   
    private String memberVar = "Some Value";
   
    public static void main(String[] args) {
        // outer class instance
        Project outerClass = new Project();
       
        // compiler error - cannot instantiate without enclosing instance.
        NonStaticProjectPK nonStaticInner1 = new NonStaticProjectPK();
       
        // compiles fine - instantiation using enclosing instance.
        NonStaticProjectPK nonStaticInner2 = outerClass.new NonStaticProjectPK();
        nonStaticInner2.callMe();

        // compiles fine - no enclosing instance neccessary.
        StaticProjectPK staticInner1 = new StaticProjectPK();
        staticInner1.callMe();
       
        // compiler error - cannot instantiate static inner class from enclosing instance.
        StaticProjectPK staticInner2 = outerClass.new StaticProjectPK();
    }

    public static class StaticProjectPK {
        public void callMe() {
            System.out.println("Hello from StaticProjectPK.");
           
            // compiler error - static inner classes can't access instance vars.
            System.out.println("memberVar: " + memberVar);
        }
    }
   
    public class NonStaticProjectPK {
        public void callMe() {
            System.out.println("Hello from NonStaticProjectPK. memberVar: " + memberVar);
        }
    }
}

_________________
"Man's Ego is the fountainhead of Human progress..."
(Ayn Rand)


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.