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: Hi And Best Regards To Everyone(Problem In Updating Table)
PostPosted: Mon Apr 16, 2007 4:05 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 12:04 pm
Posts: 25
Location: Chennai
Greetings!

Could anyone solve my follwing problem.

Act i m trying to update the table(month) for the query "update month(tablename) set name='Jeny' where monthid=2;" by using follwing Hibernate Code:-

Month m=new Month();
session.get(Month.class,m.getMonthid());

m.setMonthid(2);


/*It takes '2' as a condition for column monthid here i dont know why? If i change it to any value which is not there in table month for column monthid like '4444' then it will not update the table.*/
m.setName("Jeny");

session.update(m);

Now before The code table is like
--------------------------------------------
monthid Name
-------------------------------------------
2 John
------------------------------------------



After executing the above hibernate code the table will get updated like
--------------------------------------------
monthid Name
-------------------------------------------
2 Jeny
------------------------------------------

===============================================

Now my question is that if i want to run follwing query with the same way of hibernate code what i have used above then how should i do it?
The query is:-- "update month(tablename) set monthid=44,name="abc" where monthid=2 and name="Jeny";" [/b]




Hope i have put my question properly.If Anybody can help me out as much as soon then i will be so greatful to him.

Thanks Lot

With Regards
Saurav Jain

_________________
I Really want to share my knowledge and learn from you all people specaily about Hibernate.. Thanks Lot


Top
 Profile  
 
 Post subject: Re: Hi And Best Regards To Everyone(Problem In Updating Tabl
PostPosted: Mon Apr 16, 2007 9:05 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

without your mappings it is hard to say for sure, but I assume monthid is the id-property of your Month-class?

Then there's quite a few queer things about your code....
1.) If you want to get() a certain row and you know its id-attribute you should use
Code:
Month m = session.get(Month.class,2);

to really get the object representing your id-value.
2.) DON'T change the id-attribute of a persistent instance. That is really, really bad. For hibernate different id-values means differente instances. That's why the row doesn't get updated when you change the monthid to a value that isn't in the DB (hibernate will look for a row containing that value and try to update that row).
3.) If you want to update the primary-key-values of any of your tables you should rethink your database-design. primary-keys are for unique identification of any given entity and you wouldn't want to have any entity's identity changed (this is bad style even in pure relational programming). If you need a new Row with monthid = 44 and name="abc" then insert it:
Code:
Month m2 = new Month();
m2.setMonthid(44);
m2.setName("abc);
session.save(m2);

If you no longer need the row with monthid=2 then delete it:
Code:
Month m = session.get(Month.class,2);
session.delete(m);


Regards

piet


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 9:07 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi intimate2talent,

when loading your month object into memory (session.get(Month.class,m.getMonthid());), you're getting a java OBJECT with a specified id (e.g. hashcode). During a hibernate session, hibernate synchronizes your java OBJECT with your database ROW. So your object is mapped and BOUND to that row. You cannot just change the id as you would do in a SQL Statement. So you must insert a new row with the id 4444 and delete the one with id 2.
Hope that helps

chucky.

Don't forget to rate
---------------------------



Month m=new Month();
session.get(Month.class,m.getMonthid());

m.setMonthid(2);

/*It takes '2' as a condition for column monthid here i dont know why? If i change it to any value which is not there in table month for column monthid like '4444' then it will not update the table.*/
m.setName("Jeny");

session.update(m);


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.