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?
|