-->
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: How to map to an interface
PostPosted: Tue Nov 20, 2007 4:44 pm 
Newbie

Joined: Tue Nov 20, 2007 4:30 pm
Posts: 3
I've read through the documentation, and searched the forum, but can't find an answer to my problem with declaring a mapping to an Interface. I apologise if the answer is, in fact, obvious!

I have an existing Hibernate hierarchy (say a superclass S with subclasses A, B and C) - mapped with joined-subclass. I now want to say that A and B implement a particular interface X (just a flag really - it delares no methods) and that C references a Set of X.

If I just declare the mapping to X for the Set in C, then the DDL creation tool complains that X isn't defined - but I can't include a definition of X, because how would I say that it is implemented by A and B (they're already in a hierarchy!). The 'any' mapping looks promising, but it seems it can't be used in a Set.

Am I missing something obvious ? Or is this just not possible ?

I could turn X into a concrete class, with references to A and B, but this feels like a kludge and that there should be a more elegant way of doing it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 4:59 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
Try looking into Interceptors... with interceptors you can map the interface just like a class and then in the interceptor you can provide either a proxy implementation (see http://www.hibernate.org/339.html and check out the tests in the hibernate src - i believe the tests were something like dynamic-entity) or you can provide a factory implementation to return the concrete class (this would allow you to use whatever methodology you want to decide if the concrete impl A or B should be used).

I do not know if this will solve your problem, but I found that interceptors allowed me to map interfaces and still be able to dynamically determine the implementation using a custom factory.

I hope this helps give you some direction.

-Paul


Top
 Profile  
 
 Post subject: Re: Interceptor
PostPosted: Tue Nov 20, 2007 5:09 pm 
Newbie

Joined: Tue Nov 20, 2007 4:30 pm
Posts: 3
Looks interesting, but how would it work if my interface is just a flag (ie like Serializable). The id field looks integral to the class/interface definition, but my classes which implement the interface are already in a Hibernate hierarchy and so already have their own id fields.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 5:22 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
You are right... I don't think it would. I think I misunderstood what you are trying to do. You aren't actually trying to instantiate the interface, you just want it to be part of the mapping so that you can hold a reference to the impls as the interface.

Have you tried inheritance mapping (with the subclass tag)? You might be able to declare the parent class as abstract (to prevent hibernate from trying to instantiate the class). Again, not sure if this will help...

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 5:27 pm 
Newbie

Joined: Tue Nov 20, 2007 4:30 pm
Posts: 3
What blocks inheritance mapping is that the classes implementing the interface are already declared in a hibernate inheritance hierarchy - I'd end up having two definitions of the same class, but with different superclasses!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 6:18 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
I see what you are saying and it sounds like a bit of a conundrum. I am sorry, but I (as a newbie) am fresh out of ideas.

Good luck and sorry I couldn't be more help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 8:34 am 
Newbie

Joined: Mon Apr 09, 2007 12:25 pm
Posts: 19
Location: Boston, MA
mclenaghan,

Were you able to find a solution to your problem? I have something similar going on. Coding to interfaces, I have the following (a silly example, but the real example's more complex):

Code:
public interface SupportsTermination {

   void setTermDate(Date date);
   Date getTermDate();

}

public class Teacher implements SupportsTermination {
   
   long id;
   String name;
   Date startDate;
   Date endDate;
   
   ...
   
   public void setTermDate(Date date){
      this.endDate = date;
   }
   
   public Date getTermDate(){
      return this.endDate;
   }
}

public class Janitor implements SupportsTermination {
   
   long id;
   String name;
   Date startDate;
   Date endDate;
   
   ...
   
   public void setTermDate(Date date){
      this.endDate = date;
   }
   
   public Date getTermDate(){
      return this.endDate;
   }
}


The idea being that a termination service would be able to perform terminations for any type that implements TerminationAware.

I initially intended to utilize implicit polymorphism, expecting that the following query might work (I'm using Spring's HibernateTemplate) without any additional mapping:

Code:
List<TerminationAware> terms = this.hibernateTemplate
            .find("from TerminationAware");


But I get the following error message:

Quote:
org.hibernate.hql.ast.QuerySyntaxException: TerminationAware is not mapped [from TerminationAware]


So it seems some mapping is necessary. I've read over this section of the reference manual http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#inheritance-strategies several times, but I'm still unclear on how to map this interface.

Any suggestions would be greatly appreciated!

Thanks,
Leo


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.