-->
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.  [ 10 posts ] 
Author Message
 Post subject: can I do this?. About assigned generator and saveOrUpdate().
PostPosted: Mon Feb 14, 2005 3:04 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
Hi!, I have a simple class with the following xml,
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="credito.Agencia" table="T_Credito">
<id name="codigo" column="P_Codigo" type="long">
<generator class="assigned"/>
</id>
<property name="descripcion" column="A_Descripcion" type="string"/>
</class>
</hibernate-mapping>

I want to save-update this objects in the DB with the saveOrUpdate() method.
I know that I should use (for example) Interceptors because the generator class is "assigned",
but this strategy don't work for my scenario. For example, I wanto to do this:

....
Agencia a = new Agencia();
a.setCodigo(1234);
a.setDescripcion("Test");
session.saveOrUpdate(a); //Here, the insert works fine and Object 'a' is inserted in DB.

then in another execution,

....
Agencia a1 = new Agencia();
a1.setCodigo(1234); //Obs: is the same id.
a1.setDescripcion("Test modif");
session.saveOrUpdate(a1); //I hope that Hibernate try to update 'a1' since the primary key already exists in the DB.

in this point Hibernate throws an Exception of "Violation of PK constraint".

How can I do to resolve this problem?
Thanks.

[b]Hibernate version:[/b]
2.1.8
[b]Mapping documents:[/b]

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 14, 2005 3:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You have to give Hibernate a way to determine the unsaved state, either don't use assigned, use an interceptor or use unsaved-value on timestamp/version


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 14, 2005 5:12 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
[quote="michael"]You have to give Hibernate a way to determine the unsaved state[/quote]

This is my problem!, because when I want to persist an object in the DB, I don't know if my object is transient or not (since this object is created before persist it). I could execute a query to verify if the 'id' already exists in the DB and do an update or insert when it don't exists but I want to Hibernate do this for me. On the other hand, I need the id's assigned therefore I can't use other type of generator class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 14, 2005 5:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
select-before-update="true"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 14, 2005 8:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Actually select-before-update does not help in HB 2.1

You must implement Interceptor.isUnsaved().

HB3 always does the right thing "automatically".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 12:07 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
> You must implement Interceptor.isUnsaved().
But...how implement isUnsaved() if I don't know when the object is transient or persistent? since I create objects all the time and try to persist it. For example let us suppose that exists a record in the DB with PK = 1234, then I create an object b:
...
Agencia b = new Agencia();
b.setCodigo(1234);
b.setDescripcion("Test modif");
session.saveOrUpdate(b); //In this point I don't know if already exists a persist object with PK = 1234. In this particular case the objects exists so I hope that Hibernate update the record otherwise I hope that Hibernate 'insert' the object in the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 15, 2005 2:00 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
finally, I resolved the problem, simply I implemented the Michael's suggestion: use select-before-update = true. Then all works fine. The final xml is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="credito.AgenciaCredito" table="T_CreditoAgencia" select-before-update="true">
<id name="codigo" column="P_Codigo" type="long">
<generator class="assigned"/>
</id>
<property name="descripcion" column="A_Descripcion" type="string"/>
</class>
</hibernate-mapping>

Thanks.


Top
 Profile  
 
 Post subject: Re: can I do this?. About assigned generator and saveOrUpdat
PostPosted: Wed Mar 09, 2005 11:46 am 
Regular
Regular

Joined: Wed Sep 10, 2003 2:26 pm
Posts: 56
Location: San Diego, CA
Jean Cartier wrote:
Hi!, I have a simple class with the following xml,
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="credito.Agencia" table="T_Credito">
<id name="codigo" column="P_Codigo" type="long">
<generator class="assigned"/>
</id>
<property name="descripcion" column="A_Descripcion" type="string"/>
</class>
</hibernate-mapping>

I want to save-update this objects in the DB with the saveOrUpdate() method.
I know that I should use (for example) Interceptors because the generator class is "assigned",
but this strategy don't work for my scenario. For example, I wanto to do this:

....
Agencia a = new Agencia();
a.setCodigo(1234);
a.setDescripcion("Test");
session.saveOrUpdate(a); //Here, the insert works fine and Object 'a' is inserted in DB.


I don't understand how this can work. How does Hibernate know to generate INSERT instead of UPDATE for the first object? Could someone explain? thanks

Dmitry


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 1:19 pm 
Beginner
Beginner

Joined: Mon Nov 29, 2004 5:34 pm
Posts: 35
Quote:
I don't understand how this can work. How does Hibernate know to generate INSERT instead of UPDATE for the first object? Could someone explain? thanks

Dmitry



There is very good explanation to this in
http://www.hibernate.org/hib_docs/reference/en/html_single/#manipulatingdata-updating-detached

srigold


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 2:21 pm 
Regular
Regular

Joined: Wed Sep 10, 2003 2:26 pm
Posts: 56
Location: San Diego, CA
srigold wrote:
Quote:
I don't understand how this can work. How does Hibernate know to generate INSERT instead of UPDATE for the first object? Could someone explain? thanks



There is very good explanation to this in
http://www.hibernate.org/hib_docs/reference/en/html_single/#manipulatingdata-updating-detached


srigold, look carefully at Jean's code and comments. He is not updating a detached object, he is persisting a transient: a new object with an assigned key is being persisted using saveOrUpdate(). As far as I understand Hibernate, this shouldn't have worked. Yet the comments say that it did.


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