-->
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.  [ 2 posts ] 
Author Message
 Post subject: Cascade saving new item in collection twice
PostPosted: Mon Oct 30, 2006 11:45 am 
Newbie

Joined: Wed Oct 25, 2006 12:53 pm
Posts: 1
Hibernate version:
3.2 GA and annotation 3.2 GA

Mapping documents:
@Entity
public class Entity {
// Has a DB generated id

@OneToMany (mappedBy = "entity", cascade = CascadeType.ALL)
private List<Thing> things;

// Getters and setters
}

@Entity
public class Thing {
// Has a DB generated id

@OneToOne(optional = false)
@JoinColumn(name = "entity_id", nullable = false)
private Entity entity;

// Getters and setters
}

Code between sessionFactory.openSession() and session.close():
/* there are entities and things in the database already. Entity 1 has 3 things. */
Entity entity = session.load(Entity.class, 1);

Thing thing = new Thing();
thing.setEntity(entity);

entity.getThings().add(thing);

session.update(entity);
session.flush();

Name and version of the database you are using:
MySql 5.0 - InnoDB tables

The generated SQL (show_sql=true):
Two insert statements are generated.

Using this code (well, this is a trimmed down version) saves the newly created object twice (total of 5 objects for entity 1). This was verified by looking at the DB afterwards. However if you reference an item in the things collection before the update, or call .size on the collection, it is only saved once.

Is this a bug in the hibernate proxying, or am I doing something wrong?


Top
 Profile  
 
 Post subject: duplicate inserts
PostPosted: Sat Jan 27, 2007 6:44 am 
Newbie

Joined: Sat Jan 27, 2007 6:18 am
Posts: 1
I came across this problem yesterday whilst testing some code. I've found that changing the cascade type to MERGE resolves the issue but cannot understand why it is happening in the first place. The other interesting thing is that if I run the code in debug mode with a cascade type of ALL the problem doesn't occur.

Can you tell me if you have found any reasons why this problem occurs.

Thanks

Stu.


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