Hibernate updates @Version property of entity when adding items to @OneToMany collection - is this normal?
I have two Foo and Bar entities which have a 1:n relationship:
@Entity
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "foo_id")
Integer id;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "foo_id")
private Set<Bar> bars;
@NotNull
@Version
@Temporal(TemporalType.TIMESTAMP)
@Column(name="update_date", nullable=false)
private Date updateDate;
// getters and setters ...
}
@Entity
public class Bar{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "bar_id")
Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "foo_id")
private Foo foo;
// getters and setters ...
}
Now I add a Bar to an 开发者_运维技巧existing Foo:
Foo foo = entityManager.find(Foo.class, 1);
Bar bar = new Bar();
entityManager.persist(bar);
foo.getBars().add(bar);
entityManager.flush();
The strange thing is that foo.updateDate gets updated when I call entityManager.flush(), even though the only thing that should happen in the database is an INSERT of a new row to Bar - i.e. the existing Foo row should not be modified. Is this behavior of the Hibernate EntityManager normal / intentional?
精彩评论