-->
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.  [ 6 posts ] 
Author Message
 Post subject: Object saving question
PostPosted: Fri Sep 28, 2007 6:57 pm 
Newbie

Joined: Fri Sep 28, 2007 6:28 pm
Posts: 4
Hi, I have a little doubt.
Suppose I have a Question class and I do this:
******************
Question q = new Question();
q.setText("question1");
getSession.save(q);
*******************
that should persist the question in the db.
then in another moment(session) I repeat the same thing.

Then in my DB i get:
id text
1 question1
2 question1
BUT I want to get:
id text
1 question1

Please tell me I m doing something wrong. Thanks!

This is my mapping:
Code:
<hibernate-mapping>
    <class name="Question" table="QUESTION">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
        <property name="text" type="string"></property>
    </class>
<hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 7:45 pm 
Newbie

Joined: Fri Sep 28, 2007 7:40 pm
Posts: 9
Options:
1. You can make that setText little intelligent to identify this scenario i.e setText will check the DB and if the value is alreay present it will do a save
2. You can go for composite key. Programming may be little tough if you just started hibernate

Thanks,
Venkat


Top
 Profile  
 
 Post subject: reply
PostPosted: Fri Sep 28, 2007 10:24 pm 
Newbie

Joined: Thu Aug 30, 2007 1:05 pm
Posts: 7
Location: Bangalore


Little yhing u have to do
is
in before storing into DB u cross check the DB data ,meaning that get the list of the table QUESTION,and check the column text then if it is not available then add the record into the DB.....


I think iots the better thing we can do in programatically



_________________
Kumar Phani


Top
 Profile  
 
 Post subject: Object saving question
PostPosted: Wed Oct 03, 2007 7:58 am 
Newbie

Joined: Fri Sep 28, 2007 6:28 pm
Posts: 4
I would like to do the composite way you ve told me. I think its the right way.
Could somebody provide some help for doing this?
thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 8:18 am 
Newbie

Joined: Mon Oct 01, 2007 8:59 am
Posts: 18
By using composite key you can not resist the second entry.
You make the property
unique="true"
it will solve your problem.
<hibernate-mapping>
<class name="Question" table="QUESTION">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="text" type="string" unique="true"></property>
</class>
<hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 9:29 am 
Newbie

Joined: Fri Sep 28, 2007 6:28 pm
Posts: 4
ganga_tanwar wrote:
By using composite key you can not resist the second entry.
You make the property
unique="true"
it will solve your problem.
<hibernate-mapping>
<class name="Question" table="QUESTION">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="text" type="string" unique="true"></property>
</class>
<hibernate-mapping>


I think that your solution wil cause a UNIQUE constraint violation when I save the second object...
No better solution?


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