-->
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.  [ 13 posts ] 
Author Message
 Post subject: Interface mapping?
PostPosted: Thu Nov 20, 2003 4:18 pm 
Beginner
Beginner

Joined: Sun Sep 21, 2003 12:19 am
Posts: 40
Hi,

In the system I am working on we use interface everywhere. Can anybody show me how to write the mapping file for interface? Here is an example

Code:
interface IFruit {...}

interface IApple extends IFruit {...}

abstract class FruitImpl implements IFruit {....}

class AppleImpl implements IApple {...}

class FruitBasket {
    IFruit [] getFruits();
    IApple [] getApples();
}



Another thing we have to do is we use one .hbm.xml per class.

Thanks.


Top
 Profile  
 
 Post subject: Re: Interface mapping?
PostPosted: Thu Nov 20, 2003 5:55 pm 
Beginner
Beginner

Joined: Sun Sep 21, 2003 12:19 am
Posts: 40
A little bit more clarification of the problem in order to kick the tire here.

All the class mappings so far in Hibernate are based on concrete classes and interfaces are only for lazy loading rather than reference purpose I am using.

I am trying class/joined-subclass trick to make it more interface friendly (a good OO practice is acutally code on interface rather than class). Declare the class name as the interface class and declare the concrete implementation as joined-subclass.

But this will fail short for obvious reason I am specified in the question particular function call getFruit(). Everything else works perfectly.

Gavin & Co in the dev team:

Is it possible just adding one attribute in the class called interfacename, so that when I do "many-to-one" point to interfacename you know which class to go? It acts as alias to the class.


Maybe I overlooked whole a lot of issues here? Like how do you know which table to join if I ask getFruit(), right?

Thanks.


Top
 Profile  
 
 Post subject: meta attributes sound like what you're looking for...
PostPosted: Thu Nov 20, 2003 6:41 pm 
Regular
Regular

Joined: Sun Sep 21, 2003 11:43 pm
Posts: 85
Location: Massachusetts
Why not use the <class..> <meta> attributes?

There is:
<meta attribute="interface">IFruit</meta>
<meta attribute="implements">IApple</meta>
<meta attribute="extends">Apple</meta>

Among other VERY useful attributes. I'm keen on using the "generated-class" attribute myself.

For more info, see section 18.2.2 on the Hibernate 2.1 beta(6?) or section 17.2.2 for the 2.0.3 stable release:
http://www.hibernate.org/hib_docs/refer ... guide-s2-2

Regards,
David


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 9:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I think you need an <any> mapping here?


Top
 Profile  
 
 Post subject: Gavin, you rock!
PostPosted: Fri Nov 21, 2003 12:23 am 
Beginner
Beginner

Joined: Sun Sep 21, 2003 12:19 am
Posts: 40
gavin wrote:
I think you need an <any> mapping here?


Actually to me it is a duh! I guess the document recommends not using it so I just skipped the entire Any session. After going back to read it, it made sense.

To me it seems that many-to-one can be completely replaced by <any>? <any> seems to map to type 'generic'/object which in java is a reference anyway.

Of course the downside of it is that you mix your column value with schema (I would rather column value be the concrete implemenation java class name than the table name though). Plus a little bit inconvience for the reader of the mapping file try to understand actually object type return, this force them to go to see the Java code, but I guess that's ok.

Thanks.

P.S. hidden in my mind, I still prefer a better way of handling code by contract(interface).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I would rather column value be the concrete implemenation java class name than the table name though



This is possible, just use:

Code:
<any id-type="long" meta-type="class">
     <column name="class_name"/>
      <column name="id"
</any>


Top
 Profile  
 
 Post subject: Not quite.
PostPosted: Fri Nov 21, 2003 1:19 am 
Beginner
Beginner

Joined: Sun Sep 21, 2003 12:19 am
Posts: 40
Interesting, I skimmed through the code.

Meta-type has to be Type, CompositeUserType,UserType, serializable. I just tried Serializable (IFruit extends Serializable), it seems it serialize my entire class into table column which is not the one I want of course.

I looked at every other type of classes, all of them need to create instance. While in my case here is an interface as meta-type, so I guess it doesn't truely work, maybe I am totally wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 1:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I don't know what you're talking about.

a meta-type is a Type that takes an instance of java.lang.Class and binds a column value. It does not take an instance of the associated class.

Why didn't you try my suggestion: meta-type="class".

I posted it for a reason....


Top
 Profile  
 
 Post subject: oops!
PostPosted: Fri Nov 21, 2003 1:42 am 
Beginner
Beginner

Joined: Sun Sep 21, 2003 12:19 am
Posts: 40
Oh... Now I got it.

You meant literally 'class'. Well stupid me, trying to replace the 'class' with my interface class and it just throws me exception then I decided to figure why it throws me exception, then it leads my last post.

Now it works and CHARMINGLY, it indeed persist the class name in the db column.

Thanks Gavin for your valueable time.


Top
 Profile  
 
 Post subject: <any...> cool!
PostPosted: Fri Nov 21, 2003 2:07 am 
Regular
Regular

Joined: Sun Sep 21, 2003 11:43 pm
Posts: 85
Location: Massachusetts
Thanks for explaining the Any reference. I just tried it out and it's VERY interesting. Now, I'm more dangerous (Add manaical laughter here).

Regards,
David


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 4:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Don't get too excited by any, guys ... navigating an any association in a query is problematic. You would need to use a theta-style join like:


Code:
from Foo foo, Bar bar where foo.any.class = 'Bar' and foo.any.id = bar.id


I think its clear why this restriction exists ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:35 pm 
Beginner
Beginner

Joined: Sun Sep 21, 2003 12:19 am
Posts: 40
David,

Consider <any> just likes setXXX(Object) and Object getXXX() + run time reflection (object.getClass() ). It lost the strong type support, and not quite good OOP (I meant pass every reference as Object type).

Gavin,

I think the query can be similar to many-to-one, and under-hood you can execute one extra select to get the many side of the table name from column value, then do a join right? So to me as a user, I still see I am in a clean query.

Guess a better interface based progamming support is still on my wish-list for hibernate (each time you see an interface, you will also attach a concrete class column beside it).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
That would be VERY hard to implement :(


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