-->
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.  [ 10 posts ] 
Author Message
 Post subject: mapping generic classes
PostPosted: Wed Jul 15, 2009 3:38 pm 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
hi,

do you know any good sources concerning mapping generic classes? in google i only found this: viewtopic.php?f=25&t=995755

what dll? i'm using java. :D didnt found any information in the reference either, at least not by searching for generic. is it possible?

thx
i will give credit if you answer ;)


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Thu Jul 16, 2009 2:00 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
What do you mean by generic classes?

You can't map a class to multiple tables or maybe that's a bad way to put it. Maybe a better way to say it is you can't map a pojo property to more than one field in the database. Barring a few special annotations like "secondarytable". Even with those annotations though the class always maps to the same structure.

You can select into a class though which is very usefull for instance.

"SELECT new widget(name, description, someotherfield) FROM EmployeeType"

Then use that same widget to get information out of another table

"SELECT new widget(name, description, someotherfield) FROM ProductType"

Widget is not a mapped class, but it can be any class provided you have a constructor that takes a name as a string, description as a string, someotherfield as a string.

The datatypes are determined by what you actually mapped them to in EmployeeType or ProductType.


Hope that helps.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Fri Jul 17, 2009 7:25 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
hey thx for your answer. i'd like to rate it but i have problems find the button/link (whatever it is) to do this. :/ but i will figure out and as soon i know it i will do it.

by generic classes i mean:

Code:
class SomeClass<TypeOfAProperty> {
     private TypeOfAProperty aProperty;
     public TypeOfAProperty getProperty() {...};
     public void setProperty(TypeOfAProperty aProperty) {...};
}


now i can do the following:
Code:
SomeClass<String> exampleA = new SomeClass<String>();
exampleA.setProperty("testtest");
SomeClass<Integer> exampleB = new SomeClass<Integer>();
exampleB.setProperty(new Integer(44));


the problem is the type that can change and how will hibernate know what kind of class to instantiate when it loads a generic class from the database. and i think its getting even worse if i want to use an EntityType instead of a ValueType like in this example. is there any elegant solution in hibernate or will i have to create non-generic classes with their own mapping files? maybe i can create a generic mapping and use this to instantiate non-generic mappings?


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Fri Jul 17, 2009 9:13 am 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
I haven't run into this yet, so I can't say this for sure, but I don't think you can map multiple types to a field.

Hibernates Dialect classes, which you can look at in the source code, map database types to one type and one type only. So if you have say varchar it will map that to a string. You can't map it to an int, unless all strings map to an int(which would cause problems when "harry" can't be converted).

It sounds like you are trying to map different database structures to the same class and that is not allowed.

You can go the other way around and map multiple classes to the same database structure though.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Sat Jul 18, 2009 9:09 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
i'm just wondering if there is a convinient solution to mapping generic classes in hibernate. i know that i cant map a property that can has different types to the same table-column. but in frameworks generic classes are very common und so i just wondered, if there was a way to do the mapping conviniently, because if there is not, i will have to repeat the same mapping files over and over again, for every different type that i can assign to my type-parameter of my generic class.... even if all the other types of the class stayed the same because there were not defined in a generic way. you see the problem?

btw i really cant rate replies. i cant see a link or a button or whatever anywhere. i just have this button with the exclamation point. admins?


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Sat Jul 18, 2009 11:41 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
I haven't tried this, but perhaps this discussion will help:

viewtopic.php?f=1&t=945113

_________________
-----------------------
Paul
Software Engineer
Lockheed Martin


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Sat Jul 18, 2009 12:44 pm 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
thank you very much for the link! very much appreciated!

unfortunately this thread ends when it gets interesting. :/ but the last 2 questions are exactly the same like mine and the very last is only 3 days old. i will keep an eye on this thread. thx!


ps: if i would find the rate-link, i would rate... or has the rating system already vanished?


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Sat Jul 18, 2009 11:44 pm 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
i am thinking that it (the rating system) might have vanished.... good riddance.

i agree, the link ended when it got interesting. i myself have wanted to do what you are doing... in the thread i sent you, i do not think the hibernate developer actually understood what the person wanted to do. i am not sure it is feasible in hibernate... i think that if you got creative, you could probably make the "any" type work... but, i am not entirely sure. if you come up with anything, please share... i would love to know of a good solution.

good luck!


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Thu Jul 23, 2009 10:40 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
on the odysse to find a solution for the problem of mapping generic classes, i stumbled upon dtd-entities. those can be used to include parts of mapping files into another mapping file to realize a non-polymorphic table-per-concrete-class strategy. i thought, since there are parameterizable entities, those could be used to solve the mapping of generic classes problem. but i was wrong. why that is the case, how the dtd-solution works and my conclusions to this approach can be found here.

unfortunatly, hibernate has no real support for non-polymorphic table-per-concrete-class inheritance and no support for mapping generic classes at all.


Top
 Profile  
 
 Post subject: Re: mapping generic classes
PostPosted: Fri Jul 24, 2009 11:12 am 
Beginner
Beginner

Joined: Mon Nov 19, 2007 4:40 pm
Posts: 46
that has been my basic conclusion as well. you can map a generic class if you are willing to map the instance with the specific type... e.g.

Person<?> can't be mapped
Person<SomeObject> can be mapped since the runtime type is known.

I really hope that someone at hibernate comes up with a good way to handle this.

-Paul

_________________
-----------------------
Paul
Software Engineer
Lockheed Martin


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