-->
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.  [ 8 posts ] 
Author Message
 Post subject: Pattern of Interface Oriented Programming with Hibernate?
PostPosted: Wed Dec 28, 2005 9:05 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
When using Interface Oriented Programming, most object is referenced by interface. Let me start with a simple example:
Say we have two interface and two implementing classes, they are in a one-to-one relationship.
Husband, Wife, HusbandImp, WifeImp

public interface Husband{
Wife getWife();
}

public interface Wife{
Husband getHusband();
}

public class HusbandImp implements Husband{
public Wife getWife(){
return _wife;
}
private void setWife(Wife wife){
_wife = wife;
}
}

public class WifeImp implements Wife{
public Husband getHusband(){
return _husband;
}
private void setWife(Husband husband){
_husband = husband;
}
}

note that only the imps have the access to setters while they all only talks to interfaces. What is the best pattern for hibernate to map Husband and Wife? The only solution in my mind is as the following:
<class name="Husband">
<subclass name="HusbandImp">
<property name=Wife Class=Wife>
</subclass>
</class>
<class name="Wife">
<subclass name="WifeImp">
<property name=Husband Class=Husband>
</subclass>
</class>

Note the property is mapped in the implementation class rather than the Interface. I don't like this pattern since the interface in the mapping file hardly do anything. And I will need an extra empty table for that interface. Using a descriminator column will be better, but we will lose that option if we want to use table-per-class in the inheritance hierachy.
Is there any better sollution? Many Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 1:14 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use the implementation classes as you describe, but add
Code:
<meta attribute="implements">InterfaceName</meta>

in your mappings. You don't need to refer to the interfaces too heavily in your hibernate mappings: after all, hibernate is not "outside" your code's domain and should be allowed to see all the implementation details (that is, setters/mutators) that code outside the domain/API cannot.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 10:08 am 
Beginner
Beginner

Joined: Tue Dec 28, 2004 11:30 am
Posts: 32
Ok tenwit , now that said if proxies are created are they gonna be extending the Interface because you included that line or are they going to subclass the implementation class ??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 4:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The proxies work off the concrete class, but that class implements your interface so the proxy does, too.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 6:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you guys do know that the meta attributes has nothing to do with the runtime mappings used by hibernate, right ?

the meta attribute is only used when the hbm2java code generator is running on the code.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 6:08 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes. I recommend using is as it amounts to self-documenting code. It shows that the mapping wants to create an object of InterfaceType, but knows that it's not allowed because it has to use concrete classes.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:58 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
tenwit wrote:
Use the implementation classes as you describe, but add
Code:
<meta attribute="implements">InterfaceName</meta>

in your mappings. You don't need to refer to the interfaces too heavily in your hibernate mappings: after all, hibernate is not "outside" your code's domain and should be allowed to see all the implementation details (that is, setters/mutators) that code outside the domain/API cannot.

say when persisting husband's wife property, hibernate will get the Wife interface instead of the WifeImp class. Will hibernate recognized the interface just because I added that meta attribute?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It has nothing to do with the meta attribute, that's just a "good idea". Hibernate knows that it's a WifeImp because that's what you pass to it. The fact that you pass an object that your code is treating as a Wife is irrelevant: it is a WifeImp, so obj.getClass() will return WifeImp.class, and hibernate knows how to deal with this.

All of this is standard java, and has very little to do with hibernate. I suggest reading the javadocs for Object, Class and ClassLoader, and any other material about java Class objects that you can find.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.