11

I've got working my JPA events (postUpdate) and they are triggering correctly when I update a property on my entity except for the ones that are mapped as @ElementCollection.

Is this a restriction? A configuration option?

Here is part of my entity

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Pckg {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false, length = 100)
    private String title;

    @ElementCollection
    @CollectionTable (
        name = "PckDest",
        joinColumns = @JoinColumn(name = "package_id", nullable = false)
    )
    @Column(name = "destination", nullable = false, length = 150) 
    private List<String> destinations;
    ...

In other words, if I change "title" the change is catched by my listener, but the same does NOT occur when I change "destinations"

I'm using JPA with hibernate (4.0) as provider through spring (3.1)

Thanks

3
  • Can you be more specific for when you say you change "destinations"? Are you replacing the list or modifying the list?
    – pgreen2
    Nov 29, 2012 at 2:52
  • @pgreen2 thx, when I said change I meant either way, an element on the list e.g destinations.add("LA"); or just setting a new list e.g. destinations = new ArrayList()... then I called EntityManager.save(xx). None of the above fire my onPostUpdate(), while doing Pckg.setTitle("new title") does fire my Listener correctly
    – maverick
    Nov 29, 2012 at 3:29
  • I took a brief look at the spec (download.oracle.com/otndocs/jcp/persistence-2.0-fr-oth-JSpec) and nothing jumps out at me as being wrong. I did notice a couple notes about when the events occur. They could happen when the modification occurs or when the change is flushed. Also, the spec doesn't state what should happen if an entity is modified and then removed or added and then modified. It doesn't sound like these edge cases are your problem. I don't have a test bed setup. Can you post your listener code and test code?
    – pgreen2
    Nov 29, 2012 at 4:13

3 Answers 3

2

Finally we end solving this adding optimistic locking @Version, that forces hibernate to write to the main parent table and our listeners get called.

In any case, this is still not working the way it's supposed to work, but bottom line it doesn't hurt to have the optimistic locking in place either

Thanks

1

No update is made on the owner side (Pck table), what gets updated is the table which holds the collection data (PckDest).

I think is even arguable that changing the contents of the collection could be considered an update, since it could and will often be implemented as a delete and insert.

I think you have to handle such behavior outside the entity management life cycle, or have some kind of field inside the owner entity which get's updated on a list change (like some kind of checksum), so that when you change the list also the parent has to be updated (though I don't know if this is such a good idea).

1
  • thx, actually I ended adding a optimistic locking to the main table and that makes my listener finally get called, when it compares the versions. Still no reason why this is not working the way it's supposed to work.
    – maverick
    Dec 5, 2012 at 4:03
0

Maybe this link helps you:

eventlisteners using hibernate 4.0 with spring 3.1.0.release?

Looks like there's some kind of change for Hibernate 4 and registering EntityListeners.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.