Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need advice on updating exisitng record

 
Xavier George
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a basic questions regarding loading existing record and performing update on that.

My DB currently has around 50,000 records and I want to load an existing record using 3 three keys and then I want to perform update on that. There are number of ways to do that but I seek advice from expert whether shall I write HQL or simple session.load to load a record and then session.saveorupdate(). Or any other optimized way to do that.

I do not have composite-key class but I want to load using 3 keys

Thanks for reading my post.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it depends on how many records you want to bring back. With load you can only bring back one record, plus you it takes a "String and a Long" so you can't pass it three keys. But just in general if you only want one record back and that is all that comes back then using HQL or Load doesn't make a difference. Hibernate will still create the actual SQL statement that goes back to the database. However, for the first run there might be a difference, simply because Hibernate can create the prepared statement for lookup by IDs when it first reads the mappings.

Mark
 
Xavier George
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

I have to load only one record but I must have to use THREE KEYS (business reqmt, those 3 keys will always return one record). Even, I was looking into Session API and load method expects only one key so in this scenario load is not feasible. Then, I think I have rely on HQL.

Other question regarding the update, once I fetch the record from database I will call setter of business objects needs to be updated and then finally session.saveOrUpdare(object). So. is this the right way to perform update ? or is there other optimized way to do that because I have around 23 child and child objects associated with parent object. And, one call session.saveOrUpdate(parentObject) will update entire collection data as well.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have to load only one record but I must have to use THREE KEYS (business reqmt, those 3 keys will always return one record). Even, I was looking into Session API and load method expects only one key so in this scenario load is not feasible. Then, I think I have rely on HQL.


An entity cannot have three keys. That would not be valid relational data.

You are either trying to find data based on the value of three fields or based on a single, composite primary key made up of three fields. If it is the former you are not guarenteed to get back only one record, which is why the load methods only expect one key. If its the later then the composite id is the thing you pass to the load method.

You will need to use HQL or Criteria to get your record.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The entity can have a "surrogate" PK and a "business key" composed of three fields. Then you need to create a Criteria/Query and set all the 3 values.

Pascal
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly - it can have one or the other, not both. If your Primary Key is a surrogate key then you can't guarentee that your other "key" (made up of three other attributes of the entity) will return just one record. If you add an index to these three attributes to force uniqueness you are just creating a composite key by the back door, which renders your surrogate key redundant. Calling both "keys" is misleading - an entity can only ever have one Primary Key.
[ November 06, 2006: Message edited by: Paul Sturrock ]
 
Halcon Guatemala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to @ll, I've understood several approaches by using composite primary keys/surrogate key, etc. But, I'd like to know when is good option to use surrogate key or when is preferrable to use a composite class agrouping table fields that do primary key?

Thanks in advance
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use a surrogate key. Its far simpler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic