-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 4:45 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
Hi everybody,
I have a question: can Hibernate map interfaces?
If it is possible, can someone tell me why I get an InstantiationException during loading entities from DB?
Code:
SEVERE: Exception sending context initialized event to listener instance of class it.innove.indesk.web.context.ContextLoaderListener
Caused by: org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: it.innove.security.entities.IPasswordPolicyScript

where
Code:
it.innove.security.entities.IPasswordPolicyScript
is my interface.

Thanks,
Stefano


Top
 Profile  
 
 Post subject: Re: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 9:51 am 
Newbie

Joined: Sun Jul 18, 2010 10:15 am
Posts: 8
I'm pretty sure it's possible. I'm doing it in the project I'm working on, even though I'm having some other issues...

anyway, do you have properties in your interface mapping? if so, make sure you have getters and setters defined for each in your interface code.

also,
include your mapping, does your interface mapping have a join-subclass?


Top
 Profile  
 
 Post subject: Re: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 10:11 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
Hi schwiet,
Yes, all properties referenced in the mapping file have a getter and setter defined in the proper interface. I'm sure because it is an old mapping that mapped classes; now I'm trying to map the interfaces for scalability.

Sometimes I have joined-subclasses, sometimes no.
Here are a couple of examples of my mappings
Code:
<hibernate-mapping>
   <class
      name="it.innove.indesk.entities.ILanguage"
      table="languages">

      <id
         name="id"
         column="id">
         <generator
            class="guid" />
      </id>
      
      <property name="name" column="name" unique="true" not-null="true" />
      
      <property name="fullName" column="full_name" not-null="true" />
      
      <property name="deleted">
         <column name="is_deleted" default="false" />
      </property>
      
   </class>
</hibernate-mapping>

----------------------------------

<hibernate-mapping>
   <class name="it.innove.indesk.entities.IPerson" table="persons">
      <id
         name="id"
         column="id">
         <generator
            class="guid" />
      </id>

      <property name="email" column="email" />
      <property name="externalKey" column="external_key" />
      <property name="firstName" column="first_name" />
      <property name="lastName" column="last_name" />
      
      <set
         name="userAttributes"
         inverse="true"
         order-by="attribute asc"
         fetch="join">
         <key column="person" />
         <one-to-many
            class="it.innove.indesk.entities.IUserAttribute"/>
      </set>
      
      <set
         name="referrals"
         table="users_referrals"
         fetch="join" >
         <key column="user" />
         <many-to-many column="referral" class="it.innove.indesk.entities.IIncidentReferral" />
      </set>
      
      <property name="deleted">
         <column name="is_deleted" default="false" />
      </property>
      
      <many-to-one
         name="company"
         column="company"
         class="it.innove.indesk.entities.ICompany"
         fetch="join"
         not-null="false" />
      
      <joined-subclass name="it.innove.indesk.entities.IUser" table="users">
         <key column="id" />
         <property name="username" column="username" />
         <property name="salt" column="salt" />
         <property name="accountNonExpired" column="account_non_expired" />
         <property name="accountNonLocked" column="account_non_locked" />
         <property name="credentialsNonExpired" column="credentials_non_expired" />
         <property name="enabled" column="enabled" />
         <property name="admin" column="is_admin" />
         <set
            name="roles"
            table="users_roles"
            lazy="true">
            <key column="user" />
            <many-to-many column="role" class="it.innove.indesk.entities.IRole" />
         </set>
         <set
            name="userPreferences"
            inverse="true"
            order-by="preference asc"
            fetch="join">
            <key column="user" />
            <one-to-many
               class="it.innove.indesk.entities.IUserPreference"/>
         </set>
      </joined-subclass>
      
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 10:15 am 
Newbie

Joined: Sun Jul 18, 2010 10:15 am
Posts: 8
maybe I'm misunderstanding your question, are you trying to map IPasswordPolicyScript? if so, the mapping you included did not show that mapping.


Top
 Profile  
 
 Post subject: Re: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 10:36 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
Sorry, I attached two other mappings because they are very similar and cause the same problem when selecting (I ran some test on some other entities to undertand if the problem was only in IPasswordPolicyScript mapping).
Here is the mapping of IPasswordPolicyScript:
Code:
<hibernate-mapping>
   <class name="it.innove.security.entities.IPasswordPolicyScript" table="policies">
      <id name="id" column="id">
         <generator class="guid" />
      </id>
      <property name="name" type="string">
         <column name="name" sql-type="varchar(255)" />
      </property>
      <property name="runnable" column="runnable" type="boolean" />      
      <property name="script" type="string">
         <column name="script" sql-type="text" />
      </property>
      <set
         name="passwords"
         table="passwords_on_policies"
         lazy="true"
         fetch="join">
         <key column="policy" />
         <many-to-many column="password" class="it.innove.security.entities.IPassword" />
      </set>
      
      <property name="deleted">
         <column name="is_deleted" default="false" />
      </property>
   </class>
</hibernate-mapping>


Stefano


Top
 Profile  
 
 Post subject: Re: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 10:43 am 
Newbie

Joined: Sun Jul 18, 2010 10:15 am
Posts: 8
couple of questions,

I see that in Persons you have a joined sublcass users, is users a class or another interface? If an interface mapping has no subclasses defined in a joined subclass, I'm not sure how the interface could be instantiated when loading it from the database since it will call some default constructor. an interface can't be instantiated and my guess is that if hibernate doesn't have a reference in the mapping to the actual class that was saved, then it will try to instantiate the interface that was mapped. I could be wrong, but for instance what class is implementing IPasswordPolicyScript? is there a reason you are not including it in IPasswordPolicyScript's mapping?


Top
 Profile  
 
 Post subject: Re: Hibernate and interfaces
PostPosted: Wed Jul 21, 2010 11:04 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
User is another interface. I would like to use only interfaces.

I also think that the problem is the lack of the constructor, but I thought there was some ways to tell Hibernate to use other methods to construct object instead of Class.newInstance().

The reason why I'm trying to avoid to put the concrete classes (like PasswordPolicyScript) in the mapping is because of SpringAOP.
It creates some Proxy-class around the entities. The Proxy still implements the interface (ie IPasswordPolicyScript), but it is not recognized as an instance of the class. As a result, if I map concrete classes, I get exceptions when saving entities, because the Proxy classes are not recognized as mapped entities.
That is why I'm mapping only interfaces.


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