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.  [ 1 post ] 
Author Message
 Post subject: Cascade behaviour
PostPosted: Wed Dec 21, 2011 6:54 pm 
Newbie

Joined: Wed Dec 21, 2011 6:14 pm
Posts: 2
I have two classes with a one-to-many relationship
Table1 (1) ---- (n) Table2
coded like this:
Code:
class Table1 {
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "table1", fetch = FetchType.LAZY)
   private List<Table2> table2List;
...
}
class Table2 {
...
@JoinColumn(name = "`idTable1`", referencedColumnName = "id")
   @ManyToOne(optional = false, fetch = FetchType.LAZY)
   private Table1 table1;
...
}

In my code I have
Code:
Table1 t1 = new Table1();
// add Table2 records
for (int I=0;i<24;i++)
{
Table2 t2 = new Table2();
t2.setTable1(t1);
t1.getTable1List().add(t2);
}
session.beginTransaction();
session.save(t1);
session.getTransaction().commit();
session.clear();

Which generates:
- 1 Table1 Insert
- 24 Table2 Insert
as expected.
Later in the code I update t1:
Code:
t1.setSomeField(someValue);
session.beginTransaction();
session.saveOrUpdate(t1);
session.getTransaction().commit();

Which generates:
- 1 Table1 Update
- 24 Table2 Select
- 24 Table2 Update

Is that what is expected? (I expected only one Table1 Update.)
What should I do to ensure that only Table1 is updated?

Thanks
Steve


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.