-->
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.  [ 4 posts ] 
Author Message
 Post subject: Auto generation of Id for my class with BigDecimal datatype
PostPosted: Fri May 30, 2008 10:43 am 
Newbie

Joined: Sat May 03, 2008 12:30 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hi,
I have an object Answer with the field answerID(BigDecimal). The corresponding table has answerId Number as the primary key.

Can somebody guide me how to use id generator=auto for objects of Answer (with primary key as a BigDecimal)? When I use id generate= auto, it throws an exception which says that hibernate can only generate ids of int, long etc and not Big decimal.






public class Answer implements java.io.Serializable {

private BigDecimal answerId;
private Question wilCddQuestion;
private String text;

public Answer() {
}

public BigDecimal getAnswerId() {
return this.answerId;
}

public void setAnswerId(BigDecimal answerId) {
this.answerId = answerId;
}

}

public Question getQuestion() {
return this.Question;
}

public void setQuestion(Question Question) {
this.Question = Question;
}

public String getText() {
return this.text;
}

public void setText(String text) {
this.text = text;
}
}



Hibernate version:3.0

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2008 11:13:26 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.Answer" table="ANSWER" >
<id name="answerId" type="big_decimal">
<column name="ANSWER_ID" precision="22" scale="0" />
<generator class="auto" />
</id>
<many-to-one name="Question" class="com.Question" fetch="select">
<column name="QUESTION_ID" precision="22" scale="0" />
</many-to-one>
<property name="text" type="string">
<column name="TEXT" length="1000" />
</property>
</class>
</hibernate-mapping>

Full stack trace of any exception that occurs:
The error is :
When I try to use generator=auto in the mapping file for answer id, It says hibernate can only generate ids of int, long etc and not Big decimal.


Name and version of the database you are using:
Oracle 9i


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 12:18 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
That's just a bad idea in general. A big decimal for a primary key would be too vicious on your poor code and dbms. Big Decimals have scales and all that nonsense.

My only suggestion is if you need that number still there and you need it to be a decimal, make a new primary key of type long, autogenerated. And have your code increment that big decimal column separately.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 1:28 pm 
Newbie

Joined: Sat May 03, 2008 12:30 pm
Posts: 9
Hi,
I am working with a legacy database which I cannot modify. The model classes(like Answer.java) were auto generated by HibernateTools.
So I am wondering if there is a any workaround I can do from hibernate or java side for this.

Thanks for your time.

Regards,
Saravanan.V


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 1:30 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
change it to long in the hbm.xml and change the java class. The tools, while very good, aren't perfect

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


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