-->
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: data synchronization between JTable and Hibernate
PostPosted: Tue Feb 17, 2009 3:52 am 
Beginner
Beginner

Joined: Mon Feb 02, 2009 3:22 am
Posts: 26
Hi all,

I use Hibernate to querry data and then display them in JTable. I provide the users one JButton to delete a row, one to insert a row, one to save the whole JTable, but I have trouble to synchronize data in Jtable and db table.

my db table has three columns, and JTable also have 3 columns that is exactly the same.

CREATE TABLE `tb_rate` (
`expression` varchar(50) NOT NULL ,
`ratetype` varchar(30) NOT NULL ,
`rate` float NOT NULL,
PRIMARY KEY (`expression`)
)ENGINE=InnoDB DEFAULT CHARSET=gbk;

when the user select a row in JTable and click a button to delete it, I will delete this row in db at the same time, this is easy to do by finding this row in tb_rate by the primary key and delete it.

As for the insertion, I first allow the user to select a existing row and click a button to insert an empty row at above or next line and then type values for each cell, after doing that the user need to click a "save" button to save the current datas in JTable, I use session.saveOrUpdate( object ), this will insert a row if it is not in tb_rate but update it if it is already there.

since hibernate identify a record by the primary key, so if the user update a row of JTable in the expression field ( which is primary key), after he click "save", session.saveOrUpdate( object ) will insert a new row instead of update the riginal row, because the value of expression field is different from the original, the inserted row is not my purpose, my purpose is to set the new value of primary key column.

since the user can insert or update before he click "save" so it is impossible to track which row is a inserted row and which row is updated row.

Is there anyone can help me out?

Thanks a mollion.

Yhqian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 4:59 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This doesn't solve your problem, but Hibernate doesn't allow you to change the primary key of an existing object. This must be coded as a delete+insert combination.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 5:06 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Hibernate does not support to change a primary key value.

You should use a surrogate primary key, if you still want your current id-column to be updatable.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 6:15 am 
Beginner
Beginner

Joined: Mon Feb 02, 2009 3:22 am
Posts: 26
hi nordborg

if this must be coded as delete+insert , that would be dangerous if delete is executed but insert failed .

SQL allowed to update primmary key, but Hibernate dosen't. I think isnot good enough.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 6:49 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
if this must be coded as delete+insert , that would be dangerous if delete is executed but insert failed .


It's not that dangerous, becuse you are supposed to work in a transaction. If the insert fails the delete would be rolled back and the database restored to it's previous state. In any case, I think you should consider mmerder's advice to use a surrogate primary key instead (for example and auto-incrementing number) and keep this hidden (or at least read-only) from the user.

Quote:
SQL allowed to update primmary key


Well... you can do a lot with SQL, but not everything is considered good practice. Changing the primary key will not work as soon as there are rows in other tables that references the primary key that you are updating.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 7:00 am 
Beginner
Beginner

Joined: Mon Feb 02, 2009 3:22 am
Posts: 26
Hi nordborg ,


I found Query.executeUpdate() can update value of primary key,

the code is something like:

"UPDATE Rate SET expression=:newexpression WHERE expression=:oldexpression"

Query query = session.createQuery(hql);
query.setString("newexpression", "xx");
query.setString("oldexpression", "yy");
int rowCount = query.executeUpdate();

I have tested it, it works. But as you said, it woule be a problem if other tables references the primary key that I am updating


Thanks you!


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.