-->
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.  [ 5 posts ] 
Author Message
 Post subject: Does Hibernate work with member classes?
PostPosted: Mon Apr 14, 2008 5:17 pm 
Newbie

Joined: Mon Apr 14, 2008 4:04 pm
Posts: 3
I've been trying to use Hibernate to set up ORM for a member class (i.e. a class nested inside another class). But it doesn't seem to work. An example config is shown below. Does anyone know if Hibernate actually supports member classes?

For the class name in the HBM, I've tried using both "com.example.ParentClass$MemberClass" and "com.example.ParentClass.MemberClass".

The first gives class name gives the following exception:

Code:
org.springframework.orm.hibernate3.HibernateSystemException: No default constructor for entity: com.example.ParentClass$MemberClass; nested exception is org.hibernate.InstantiationException: No default constructor for entity: com.example.ParentClass$MemberClass
...


The second gives:

Code:
Invocation of init method failed; nested exception is org.hibernate.MappingException: class com.example.ParentClass.MemberClass not found while looking for property: prop1
Caused by:
...



Am I doing something wrong?


Hibernate version:
Hibernate3 (via Spring)

Mapping documents:
Code:
<hibernate-mapping auto-import="true" default-lazy="true">
   <class name="com.example.ParentClass$MemberClass">
      <id name="id" column="id" type="int" />
      <property name="prop1" column="prop1" />
   </class>
</hibernate-mapping>



[b] Java class
Code:
package com.example;

class ParentClass
{
  class MemberClass
{
    private int id;
    private int prop1;

    MemberClass()
    {
    }

    ...
}
}




Thanks!

_________________
--
Dan Jakubiec


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 11:10 pm 
Beginner
Beginner

Joined: Wed Mar 05, 2008 4:57 am
Posts: 22
Location: Bangalore,India
I executed the same example with hibernate 3.2 version. And it works without any error message.

Here is the mapping I used :

<hibernate-mapping>
<class name="bean.Parent$Member" table="Member">
<id name="id"></id>
<property name="prop"></property>
</class>
</hibernate-mapping>

Here is the Parent Class :

public class Parent
{
class Member
{
private int id;
private int prop;

Member()
{
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getProp()
{
return prop;
}
public void setProp(int prop)
{
this.prop = prop;
}
}
}

Probably try to declare Member() constructor as public.

_________________
Naresh Waswani
+91-9986461501


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 4:33 pm 
Newbie

Joined: Mon Apr 14, 2008 4:04 pm
Posts: 3
waswani wrote:
Probably try to declare Member() constructor as public.


Thanks waswani... I tried this but it made no difference. However, note that I don't get the exception until I actually try to use the MemberClass object. In this case, I am using it as the return object in a named query.

I'm not sure what else to do. I tried moving the member class outside of the parent class into a normal class, and everything works great.

_________________
--
Dan Jakubiec


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 11:46 pm 
Beginner
Beginner

Joined: Wed Mar 05, 2008 4:57 am
Posts: 22
Location: Bangalore,India
As per Basic Java Concept, to create a inner class object , you should first create the Parent object.

I think Hibernate can not judge from the mapping that it is a Inner class and hence throws the error while instantiating the object.

If you decalre the inner class as static, it should work but I don't know if making the class static would solve your business.

_________________
Naresh Waswani
+91-9986461501


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 19, 2008 1:25 am 
Newbie

Joined: Mon Apr 14, 2008 4:04 pm
Posts: 3
Thanks waswani. That's an interesting point about instantiating a member class outside of its parent class. I guess I'm not sure how Hibernate would accomplish this in practice.

So it would seem to me that ORM may not actually work for member classes. Something to ponder, I suppose.

_________________
--
Dan Jakubiec


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.