Issue Details (XML | Word | Printable)

Key: HHH-4299
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: Paolo "Nusco" Perrotta
Reporter: Christophe Vanfleteren
Votes: 2
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Hibernate Core

Generics and @Embedded doesn't work

Created: 20/Jan/07 11:14 AM   Updated: 10/Sep/09 01:34 PM
Component/s: annotations
Affects Version/s: None
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. Zip Archive generic.zip (2 kB)

Environment: Hibernate 3.2.1, Oracle 10XE
Issue Links:
Relates
 

Bug Testcase Reminder (view):
REMINDER: Bug reports should generally be accompanied by a test case
Participants: Christophe Vanfleteren, Emmanuel Bernard and Paolo "Nusco" Perrotta


 Description  « Hide

It doesn't seem to be possible to use a plain generic field as a component,

for example:

@Embedded
private Range<Integer> bounds

doesn't work (you get an "org.hibernate.AnnotationException: Property generic.GenericRange.min 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") error.

It does work when you extend the generic class in which you define the type (IntegerRange extends Range<Integer>)

for example:

@Embedded
private IntegerRange bounds

works

I've attached some classes that demeonstrate this problem. This issue was raised before in http://opensource.atlassian.com/projects/hibernate/browse/ANN-406?page=comments, but was rejected since no testcase was provided.



Emmanuel Bernard added a comment - 20/Jan/07 12:00 PM

Thing is, you have to have an IntegerRange at some point in your system, otherwise, what do you fill up setBounds() with?
And that's the same for Hibernate, it needs to have a class it can call newInstance() on when the object is hydrated.


Christophe Vanfleteren added a comment - 20/Jan/07 12:36 PM

Emmanuel,

I'm not quite sure I quite follow your argument:

I'd use Range<Integer> on the setBounds, as in

Example e = new Example();
Range<Integer> bounds = new Range<Integer>(0,10);
e.setBounds(bounds);

Hibernate can create an instance of Range using newInstance, and it can fill up the min/max fields of it, it just needs a way to know what type those fields should have when it hydrates.

If you could do something like this:

@AttributeOverrides({ @AttributeOverride(name="min", column = @Column(name="min_size"), type=@Type(type="int") ), @AttributeOverride(name="max", column = @Column(name="max_size"), type=@Type(type="int") ) })
private Range<Integer> sizeBounds;

@AttributeOverrides({ @AttributeOverride(name="min", column = @Column(name="min_date"),type=@Type(type="date") ), @AttributeOverride(name="max", column = @Column(name="max_date"),type=@Type(type="date") ) })
private Range<Date> dateBounds;

then you'd be able to use the single Range<T> class in the same entity (you could problably also guess the correct type using the Parameterized types of the fields). Kind of like you could do when using mapping files (mapping the same class as a component twice, only using different types for the fields).

As it stands now, you need to make a specialized implementation every time you plan to use a Range of a different type.


Emmanuel Bernard added a comment - 20/Jan/07 03:09 PM

You're right, I did some tests with hibernate core and we can make it play nice if we explicit the type somehow.
But the reflection layer has to discover the type properly, I don't know how complex it is.


Paolo "Nusco" Perrotta added a comment - 20/Jan/07 06:50 PM

This is almost certainly the same bug that causes <a href=http://opensource.atlassian.com/projects/hibernate/browse/ANN-526>ANN-526</a>.


Paolo "Nusco" Perrotta added a comment - 20/Jan/07 06:53 PM

Sorry, I thought embedded HTML worked in JIRA. Again:

This is almost certainly the same bug that causes ANN-526 (http://opensource.atlassian.com/projects/hibernate/browse/ANN-526). I hope to have it fixed soon.


Emmanuel Bernard added a comment - 20/Jan/07 07:04 PM

I believe this one is simpler to fix