-->
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.  [ 5 posts ] 
Author Message
 Post subject: Native ID and custom types
PostPosted: Mon Dec 05, 2005 8:38 am 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
Hi,
I'm using Hibernate 3.1rc2.
In my application i'm using a wrapper class for the primary key of every entity, e.g.

Code:
@Entity
public class ProductEJB implements Serializable {
...
@Column(name = "id")
@Id( generate = GeneratorType.AUTO)
@Type( type="EntityId")
public ProductId getId()


Class ProductId is a simple type safe wrapper for Integer and I want it to be generated natively by the database. I created apropriate custom Hibernate type, but it didn't work because of an assert in org.hibernate.id.IdentifierGeneratorFactory: ( getReturnedClass() in my case returned ProductId)
Code:
public static Serializable get(ResultSet rs, Type type)
   throws SQLException, IdentifierGenerationException {
   
      Class clazz = type.getReturnedClass();
      if ( clazz==Long.class ) {
         return new Long( rs.getLong(1) );
      }
   ...
      else {
         throw new IdentifierGenerationException("this id generator generates long, integer, short or string");
      }
      
   }


I also tried to extend post insert generator class from Hibernate, but in the end the same static member from IdentifierGeneratorFactory was called.

The only solution I found was to patch Hibernate source, so I added to org.hibernate.type:

Code:
public interface AutoGeneratedIdType {
   public Serializable getGeneratedId( ResultSet rs) throws SQLException;
}


and changed IdentifierGeneratorFactory:
Code:
...
else if ( type instanceof AutoGeneratedIdType) {
         return ((AutoGeneratedIdType)type).getGeneratedId( rs);
      }
...


Is there a better solution to my problem?

cheers, Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 5:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use a @GenericGenerator

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 07, 2005 12:53 pm 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
This is what I tried to do in the beggining, I extended your IdentityGenerator class, overriden the getResult method and annotated my type with @GenericGenerator of this class. But in AbstractEntityPersister (line: 1969) this code is called

Code:
return IdentifierGeneratorFactory.getGeneratedIdentity(
                     GetGeneratedKeysHelper.getGeneratedKey(insert),
                     getIdentifierType()
               );


that results in calling static getGeneratedIdentity method with the behaviour I have described in my first message. So my generator is never called after the insert to database and IdentifierGenerationException is thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 1:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@Id(generator="myGen")
@GenericGenerator(name="myGen", ...)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 4:55 am 
Newbie

Joined: Mon Dec 05, 2005 8:13 am
Posts: 9
Emmanuel,
My entity is annotated correctly, just like you show above. And of course generate method of my generator is called. But when this method returns IdentifierGeneratorFactory.POST_INSERT_INDICATOR constant - generator is never called again. After AbstractEntityPersister does insert to the DB, the IdentifierGeneratorFactory is called, which can only handle int, long etc. so exception is thrown.

Pawel


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