Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the trick to have my EventListeners trigger for my @ElementCollection properties?

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

like image 520
maverick Avatar asked Nov 25 '12 04:11

maverick


2 Answers

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

like image 120
maverick Avatar answered Oct 23 '22 04:10

maverick


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).

like image 40
minus Avatar answered Oct 23 '22 02:10

minus