-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate 3 mapping of Java 5 Generics
PostPosted: Fri Jul 15, 2005 8:24 am 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
How do you map a Java 5 Generic class? One idea is to write a UserType class for the Generic which is Parameterized with the type variable(s). Does anyone have any better ideas?

Thanks,
s.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 9:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
there is no special thing about mapping a generic collection - just map it as any other collection.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 10:03 am 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
I'm not asking about a generic collection. I'm asking about a generic class created by the user. As a quick example:
class MetaData<T> {
private T object;
private long metadata;
}

How would Hibernate map this type?
I think I can write a Parameterized UserType class to do it, but I was hoping that Hibernate would have built in support for this sort of thing. Or maybe there's some other way to do it.

s.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 10:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it should work like any normal class.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 10:16 am 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
you mean without a custom UserType?
how would hibernate know the type of the genericly typed data?
especially if the generically typed data is not a built in type.

thanks,
s.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 11:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
please go read about generics - they are mostly compile time beasts, not visible at runtime. Thus hibernate will see that class as a MetaData object which has an Object variable inside.

This you can map to whatever you want - just like any other java class.

if you want to refer to the same class with different types (and want to map it as an entity not as a component) then use entity-name to distinguish between them.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 11:44 am 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
Thank you for your time. It would help if you could answer these questions directly:
Is it necessary to write a UserType class to map a Generic class as a value type?
I suspect it is. If so, is it necessary to parametrize that UserType class with the Type of the generic type?

Thank you,
s.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 11:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what part of "you can map this as any class" did you not understand ?

So, yes - if you want to map it as a component or value type then yes you will need a usertype....and the generics part does not have any effect on this.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 12:09 pm 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
Well a UserType for a generic class would have to be paramerized with the type(s) of the generic type variable(s). That is not the case for non-generics. I have succeeded in writing such a UserType, but is not trivial.
s.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 12:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I have no idea of why you keep saying this.

What is so different between the mapping for a normal MetaData and your MetaData<X> ? Its *exactly* the same if you map it as a component.

<component name="blah" class="MetaData">
<property name="object" type="java.lang.String"/>
<property name="metadata" type="long"/>
</component>

That works for MetaData and MetaData<String> ...changing the type of the object is simply done by putting in a different type on the "object" property ..... if the object type is something different then of course it needs another type - but that is *no* difference between this and any other class (im starting to sound like a broken record ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 1:19 pm 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
Once again you are misunderstanding me. I am not talking about mapping a generic as a component. I am talking about writing a custom UserType for a generic class. Here's a snippet of a UserType class that one might write:

class MetaDataUserType implements UserType, ParameterizedType {
private Type type;
public void setParameterValues(Properties params) {
set type from params
}
...
}

This UserType MUST be parameterized with the Type of the generic type. This is different than a non-generic UserType and tricky to implement correctly.

s.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 1:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you wrote "How do you map a Java 5 Generic class?" and I answered.

I do not understand why you would want to write a paramaterizied type for this since component will do the same.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 1:23 pm 
Newbie

Joined: Fri Jul 15, 2005 8:18 am
Posts: 8
For the same reason that I would write any UserType class. To avoid repitition in all the places that the same generic class appears.

Thanks for your time,
s.


Top
 Profile  
 
 Post subject: java 5 hibernate mapping
PostPosted: Thu Mar 20, 2008 6:27 am 
Newbie

Joined: Thu Mar 20, 2008 6:20 am
Posts: 3
I have a question about the mapping of java 5 generic types

If i have the class
class MetaData<T> {
private Collection<T> objects;
}

Suppose i have a lot of types T saying 5 types.
What is the best way to map these 5 types ?

thanks
gea


Top
 Profile  
 
 Post subject: Re: Hibernate 3 mapping of Java 5 Generics
PostPosted: Mon Jun 15, 2009 4:43 am 
Newbie

Joined: Fri Apr 15, 2005 4:49 am
Posts: 1
I have the same or similar question.
Say I have a generic class with property soldiers:

Code:
@Entity
public class Troop<E extends Soldier> {
   List<E> soldiers = new ArrayList<E>();
   @ManyToOne
    public List<E> getSoldiers() ...
    public E createAndAddSoldier(Class<E> clazz, String name) {...


And say I have 20 subtypes of soldier.
I want to constrain the type of soldiers in a troop to one subtype (or descendant of subtype ) E .
For a findall to return correctly parameterized troop instances I guess hibernate would need a discriminator column to identify which type to substitute for E.
I haven't come across anything in JPA or Hibernate mapping config that can allow me to do this. I am looking at a solution for the one-end of the one-to-many relationship. On the many end I would use inheritance mapping (one-table) with discriminator column

Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "classtype", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("SLD")
public class Soldier {
   Troop<? extends Soldier> troop;

       @ManyToOne
   public Troop<? extends Soldier> getTroop() {
      return troop;
   }


Is there a way to make this work, have hibernate return a correctly instantiated graph for this situation? I would have no problem duplicating the classtype parameter from soldier into troop table. BUT i'd rather not create 20 subtypes of troop.

_________________
------------------------

Faizal Abdoelrahman
Amsterdam


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.