Update One-to-Many/Many-to-Many does not throw update events
Hi guys, great job with v3.1! Hibernate rocks!
I have a question about One-to-Many/Many-to-Many actions.
Here i have
class Camera {
String model;
Collection lenses = new HashSet();
Flash flash;
}
class Lens {
String model;
}
Either i map lenses as <one-to-many/> or <many-to-many/>, when i add a lens to the lenses. And then call flush (see code below), i expect a PostUpdateEvent is thrown, with the Camera as entity, right?
However, i did not receive any specifc event about the <one-to-many/> or <many-to-many/> link flush.
What do you think? Am i being unreasonable here?
Also, if i do not call flush, the link between camera and lens is not created in the database, is it an expected behavior?
Thanks a lot!
Hibernate version:
3.1
Mapping documents:
<hibernate-mapping default-access="field">
<class name="photography.Camera">
<id column="id" type="int">
<generator class="native"/>
</id>
<property name="model"/>
<set name="lenses" table="CameraLens">
<key column="cameraId"/>
<many-to-many column="lensId" class="photography.Lens"/>
</set>
<many-to-one name="flash" column="flashId" not-null="false"
cascade="all"/>
</class>
</hibernate-mapping>
<hibernate-mapping default-access="field">
<class name="photography.Lens">
<id column="id" type="int">
<generator class="native"/>
</id>
<natural-id>
<property name="model"/>
</natural-id>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Camera camera = new Camera();
camera.model = "1v";
Lens lens = new Lens();
lens.model = "IS";
session.save(camera);
session.save(lens);
camera.lenses.add(lens);
session.update(camera);
session.flush();
session.close();
protected static Configuration openHibernate(String resource) {
Configuration configuration = new Configuration().configure(resource);
configuration.addClass(Camera.class);
configuration.addClass(Flash.class);
configuration.addClass(Lens.class);
PostUpdateEventListener listener = new MockPostUpdateEventListener();
PostInsertEventListener postInsertEventListener = new PostInsertEventListener() {
public void onPostInsert(PostInsertEvent event) {
// System.out.println("onPostInsert");
// System.out.println("event.getEntity().getClass() = " + event.getEntity().getClass());
}
};
EventListeners eventListeners = configuration.getEventListeners();
eventListeners.setPostUpdateEventListeners(new PostUpdateEventListener[]{listener});
eventListeners.setPostInsertEventListeners(new PostInsertEventListener[]{postInsertEventListener});
return configuration;
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
hsql
The generated SQL (show_sql=true):
Hibernate: insert into Camera (model, flashId, id) values (?, ?, null)
Hibernate: call identity()
onPostInsert
event.getEntity().getClass() = class photography.Camera
Hibernate: insert into Lens (model, id) values (?, null)
Hibernate: call identity()
onPostInsert
event.getEntity().getClass() = class photography.Lens
Hibernate: insert into CameraLens (cameraId, lensId) values (?, ?)
Debug level Hibernate log excerpt:
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - dirty checking collections
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - Flushing entities and processing referenced collections
13:00:07 [ main] DEBUG [Collections ] - Collection found: [photography.Camera.lenses#1], was: [<unreferenced>] (initialized)
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - Processing unreferenced collections
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - Scheduling collection removes/(re)creates/updates
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
13:00:07 [ main] DEBUG [Printer ] - listing entities:
13:00:07 [ main] DEBUG [Printer ] - photography.Camera{lenses=[photography.Lens], flash=null, model=1v}
13:00:07 [ main] DEBUG [Printer ] - photography.Lens{model=IS}
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - executing flush
13:00:07 [ main] DEBUG [AbstractCollectionPersister] - Inserting collection: [photography.Camera.lenses#1]
13:00:07 [ main] DEBUG [AbstractBatcher ] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
13:00:07 [ main] DEBUG [SQL ] - insert into CameraLens (cameraId, lensId) values (?, ?)
Hibernate: insert into CameraLens (cameraId, lensId) values (?, ?)
13:00:07 [ main] DEBUG [AbstractBatcher ] - preparing statement
13:00:07 [ main] DEBUG [IntegerType ] - binding '1' to parameter: 1
13:00:07 [ main] DEBUG [IntegerType ] - binding '1' to parameter: 2
13:00:07 [ main] DEBUG [AbstractBatcher ] - Adding to batch
13:00:07 [ main] DEBUG [AbstractCollectionPersister] - done inserting collection: 1 rows inserted
13:00:07 [ main] DEBUG [AbstractBatcher ] - Executing batch size: 1
13:00:07 [ main] DEBUG [AbstractBatcher ] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
13:00:07 [ main] DEBUG [AbstractBatcher ] - closing statement
13:00:07 [ main] DEBUG [AbstractFlushingEventListener] - post flush
|