views:

38

answers:

1

Hi! I have a strange Hibernate behaviour in my program. I have two classes with onetomany relation: good and price:

    @Entity
    @Table(name="GOODS")
    public class Good {

    ....
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="GOOD_ID")
    private Set<Price> prices = new HashSet<Price>();
    ...}



    @Entity
    @Table(name="PRICES")
    public class AuctionPrice {

    ..

    @Column(name="PRICE")
    private double price;
    ...}

When I update hierarchial structure which contains goods I see such queries in hibernate sql log:

update PRICES set GOOD_ID=null where GOOD_ID=?
...
update PRICES set GOOD_ID=? where id=?

for all prices (when there are no actual changes in prices).

Why hibernate set this field to null and after it reverts it back, is it possible to avoid this?

A: 

If GOOD_ID can not be null (which I assume), you should declare it so:

@JoinColumn(name="GOOD_ID", nullable = false)
Péter Török