-->
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: Flushing without calling saveOrUpdate
PostPosted: Mon Jun 26, 2006 6:27 am 
Newbie

Joined: Mon Jun 26, 2006 4:38 am
Posts: 7
Location: Osijek, Croatia
Hibernate version: Hibernate 3.1.3

Example : I have two objects maped to DB via Hibernate, I use long sesions (session spans acros many requests ), for example user changes some properties of those two objects and than opens transaction and calls saveOrUpdate on one of the objects but hibernate saves both objects. actualy I wolud like hibernate just to update object on which I called saveOrUpdate.


Session session = HibUtil.getSession();
Class1 o1=(Class1) session.load(Class1.class,new Long(1));
Class2 o2=(Class2) session.load(Class2.class,new Long(2));

o1.setP1("Value3");
o2.setP2("Value4");
Transaction t = session.beginTransaction();
session.saveOrUpdate(o1); // inside transaction saves both objects instead only o1
t.commit();


The question is can it be avoided somehow? Kruno.


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="orka.Class1"
table="test" >

<id name="sifra" column="sifra_osobe" type="long">
<generator class="native"/>
</id>
<property name="p1" />

</class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="orka.Class2"
table="test2" >

<id name="sifra" column="sifra_osobe" type="long">
<generator class="native"/>
</id>
<property name="p2" />

</class>
</hibernate-mapping>

package orka;


class Class1 {
private long sifra;

private String p1;

public String getP1() {
return p1;
}

public void setP1(String p1) {
this.p1 = p1;
}

public long getSifra() {
return sifra;
}

public void setSifra(long sifra) {
this.sifra = sifra;
}


}
package orka;

public class Class2 {
private long sifra;

private String p2;

public String getP2() {
return p2;
}

public void setP2(String p2) {
this.p2 = p2;
}

public long getSifra() {
return sifra;
}

public void setSifra(long sifra) {
this.sifra = sifra;
}
}

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://192.168.0.234/kruno</property>
<property name="connection.username">orka</property>
<property name="connection.password">orka</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="c3p0.acquire_increment">3</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property> <!-- seconds -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- <property name="hbm2ddl.auto">update</property> -->



<mapping resource="orka/Class1.hbm.xml"/>
<mapping resource="orka/Class2.hbm.xml"/>



</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 6:47 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Quote:
Class2 o2=(Class2) session.load(Class2.class,new Long(2));


You are modifying o2 also. That is why it is also updated beacuse of "automatic dirty checking". If you use a new session for saveorupdate you will get the required behaviour.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 6:57 am 
Newbie

Joined: Mon Jun 26, 2006 4:38 am
Posts: 7
Location: Osijek, Croatia
Can automatic dirty checking be disabled becuse I need long running sessions


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 7:01 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Sorry, I didn't read your requirements correctly.

If you could reset the values to the earlier one then the object would not get updated. Another option could be to call session.evict on the remaining objects that you do not want to save. I have not used this so I am not sure how it works.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 7:07 am 
Newbie

Joined: Mon Jun 26, 2006 4:38 am
Posts: 7
Location: Osijek, Croatia
I've tried with session.clear but I am not sure of other consequnces of that.
Let me ask again because I changed my second post so You my not have noticed can automatic dirty checking be disabled?
Thanks for the trouble!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 7:21 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Don't think so.


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.