-->
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.  [ 5 posts ] 
Author Message
 Post subject: Different object instance, same record on table.
PostPosted: Mon Jan 30, 2006 5:22 am 
Hallo all, i'm pretty new.

I got a problem with Hibernate.

I got a Pojo like this one

public class Argument {
private String id;
private String key;
private String value;

......
}

In hibernate mapping the id is native.

I would like the same record in the DB when i save two Arguments with same key and value.

Instead when a create two arguments like in this code:

Argument argument = new Argument(key1, value1);
Argument argument1 = new Argument(key1, value1);

..... session.saveOrUpdate(argument);
..... session.saveOrUpdate(argument1;

I get two different records in the table Argument like this

ID KEY VALUE
1 key1 value1
2 key1 value1


Is it possible to make something like this?

Thank yuo very much.

Massimo


Top
  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 6:30 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
then you should consider to define your
private String key;
private String value;
as primary key.

Read about composite-id
and natural primary key in the reference.

When these fields become your primary keys you cannot change them using Hibernate any more.

If you want to be able to change them, you must stick to your id
and verify by your application that it first tries to find an existing Argument with your keys before inserting a new one.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 3:51 am 
Thank you for your reply.

So the problem is thata Argument is in association N:1 with a bean named Agent in this way:
Agent N <--------- 1 Argument

So every Agent has a

Collection<Argument> argument = new HashSet<Argument>();

to express this association.

Since i parse data from xml with Digester, every Agent Collection wil have different instances of the same semantic Argument.

Is the only way to process "by hand" every Collections and to fix the problem
coding a method that searches for every instance of same semantic Argument replacing all of them with the same?

Thank you again.

By bY

Massimo


Top
  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 4:07 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
If you want to avoid making a db select for each argument than you might add each argument to a HashMap during parsing. Calculate the hash from your two fields and look in the HashMap if the entry exists.

Code:
Map allArguments = new HashMap();
while (new entry in my xml){
Argument argument = createFromXML();
long hash = calculateHash(argument);
if (map.get(hash)== null){
session.save(arguement);
map.put(hash, argument);
}


You can keep your id in this case but must make sure that your calculateHash method does only take the two fields into account.

I would appreciate, if you help, when this helps.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 3:44 am 
Ok thank you, i appreciated your post and i rated them.

I also make a post of your guides to delicious :D

By by all

Massimo


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