File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes generics and hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "generics and hibernate" Watch "generics and hibernate" New topic

generics and hibernate

Ryan Ericson

Joined: Aug 09, 2011
Posts: 2

Well first of all, this is my first post. So hi! Second of all, following full disclosure, this is cross-posted on stackoverflow however there were a lack of answers so I decided to reach out further.

Given the following Java class:

I would like to be able to use generics to have type safety, while still utilizing hibernate to do my persistence. By default, I am (obviously) receiving an exception of ...has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type. What I'd like to do is specify to hibernate what classes are valid there, such as multiple annotations of the targetEntity attribute. The reason I'd like this is anything can go into that Box, not just specific things out of an inheritance hierarchy. An example would be if I have an abstract class Parent and two children ChildA and ChildB. Not only would I want ChildA and ChildB as possible members, I would also like to fill the Box with String.

Please ask me any questions if I am not making myself clear. I'm not even sure if what I'm asking is possible, but the first comment on S.O made a lot of sense as to what I wanted the final table to look like:

"How do you expect Hibernate to build such a generic object? It seems like you would have to have some sort of discriminator column and identify all the possible objects that could be used. But, I don't know if that's possible here."

Basically it would be nice to wind up with a discriminator column in the Box table, along with the foreign key of the given member in another column. (Much like how hibernate by default handles abstract classes with Entity declaration on the parent)
Waswani Naresh
Ranch Hand

Joined: May 01, 2008
Posts: 66
Hibernate is more of static configuration and I really doubt if what you are trying to achieve is really doable with current Hibernate architecture.

Naresh Waswani

Naresh Waswani
Ryan Ericson

Joined: Aug 09, 2011
Posts: 2
From my tests, it doesn't seem like hibernate is able to pick up bounded generics either? Ex: <T extends PersisatantClass>
I agree. Here's the link:
subject: generics and hibernate
It's not a secret anymore!