-->
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.  [ 3 posts ] 
Author Message
 Post subject: Java 5 Generics and Object
PostPosted: Tue Feb 16, 2010 4:04 am 
Newbie

Joined: Mon Jul 13, 2009 9:00 am
Posts: 7
I'm having problems mapping Generics in Hibernate. Our database tables are all designed the same way and each table has an identifier with ID and DATE. ID can be an Integer or a String. I created a generic class, which can accept Integer or String as valid types for the ID-field.

So far so good. However, Hibernate doesn't know the type of object when he parses the mapping file. I've done some debugging and apparently it compares the amount of columns from the mapping file to the number of fields in the corresponding object (somewhere in RootClass). When it encounters an object of type java.lang.Object, it returns a column span of 2 by default (I think it was in SimpleValue.class). Javadoc states that this is because Object is deprecated (don't know why, but ok). So the columnspan is not equal and I get an exception:

Quote:
identifier mapping has wrong number of columns


Is there a way to map a composite-id with generics? And how to do it? Or how to map a variable of type java.lang.Object?


Top
 Profile  
 
 Post subject: Re: Java 5 Generics and Object
PostPosted: Tue Feb 16, 2010 6:51 am 
Newbie

Joined: Thu Feb 11, 2010 2:05 pm
Posts: 4
Location: Ghent, Belgium
I think Hibernate must know the exact object type when you start the session.
So, perhaps the best way to implement that you want is to create a custom hibernate type.
Another easy solution I propose you is to store all as a String, so hibernate know the exact object type. Then you can add several getters and setters for integers and String, something like:

Code:
public class Sample<T>{
private String Id;

public Sample(){
}

public Sample(T id){
Id = id.toString();
}

public void setId(T id){
Id = id.toString();
}

public T getId(){
return Id;
}

}


I havnt compiled the code, is only a suggestion.
hope this help


Top
 Profile  
 
 Post subject: Re: Java 5 Generics and Object
PostPosted: Thu Feb 18, 2010 9:01 am 
Newbie

Joined: Mon Jul 13, 2009 9:00 am
Posts: 7
We've been upgrading to Hibernate 3, but the problem persists unfortunately :). Maybe a bit gullible :p.

We'll implement a workaround, but what I still don't understand is why ObjectType (or AnyType as of 3) returns a column span of 2 by default. Javadoc doesn't say a thing and documentation stays quiet about this one. Hopefully someone around here can explain this :).


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