-->
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: BigInteger ID columns
PostPosted: Sun Jan 14, 2007 3:05 pm 
Newbie

Joined: Sun Jan 14, 2007 2:52 pm
Posts: 19
I have an existing database and raw JDBC based project that I would like to transfer over to Hibernate with Annotations. I have so far written a very simple class to simluate the real project.

Code:
@Id @GeneratedValue
BigInteger id;
@Column(length = 100)
String forename;
@Column(length = 100)
String surname;
@Column(scale = 10, precision = 2)
BigDecimal money;
BigInteger bigNumber;


I am using MySQL dialect against MySQL 4.1. The ID column is "ID BIGINT UNSIGNED NOT NULL AUTO_INCREMENT".
The type of the current ID field on all tables is mapped to java.math.BigInteger.

When I try to persist an object I recieve the following error.
Quote:
this id generator generates long, integer, short or string; nested exception is org.hibernate.id.IdentifierGenerationException: this id generator generates long, integer, short or string
Caused by: org.hibernate.id.IdentifierGenerationException: this id generator generates long, integer, short or string


When I follow the advice given in the exception and use Long everything is happy and the object persists. However the MySQL documentation shows BigInteger as the recommended type to map to[1], so I would like to stick with it. Also I have a large number of references to these objects already and would prefer not to have to refactor so much code. Is there a way to continue to use BigInteger for my ID columns?

Thank you,
Martin.

[1]http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-type-conversions.html


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 14, 2007 11:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Write a new generator class that extends org.hibernate.id.IdentityGenerator, and override getResult:
Code:
return rs.getBigDecimal(1).toBigInteger();
Then specify that class in your @Id annotation (I don't know how to do that bit).

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 2:19 am 
Newbie

Joined: Sun Jan 14, 2007 2:52 pm
Posts: 19
Thank you. I will have a look around for some more examples now I have a starting point. What I currently have in the manual JDBC is (BigInteger)rs.getObject("ID"); so I should be able to continue using that in the generator class.


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.