Hi,
I hava a simple mapping :
Code:
@Entity @Table(name = "BAR")
@org.hibernate.annotations.Entity(dynamicUpdate=true, dynamicInsert=true,selectBeforeUpdate=false)
public class Bar implements java.io.Serializable {
@Id
private String id;
@ManyToOne(cascade={CascadeType.PERSIST}, fetch=FetchType.LAZY, optional=true)
@JoinColumn(nullable=false)
private Foo foo;
@Temporal(TemporalType.TIME)
private Date timeAttribut;
@Version
private Integer version;
Code:
@javax.persistence.Entity()
@javax.persistence.Table(name = "FOO")
public class Foo implements java.io.Serializable {
@Id
private String id;
private Long cpt;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
private Collection<Bar> bars = new ArrayList<Bar>();
If i get a detached instance of Bar from my ihm with the Foo id associated (this Foo id exist in DB), the code bellow works perfectly (my Bar object is updated in DB) :
Code:
@Transactional()
public void updateBar(Bar bar, String fooId ) {
Foo foo = new Foo(fooId );
bar.setFoo(foo);
em.merge(bar);
}
If i add a @Version to Foo class, the same code doesn't work anymore. I get "object references an unsaved transient instance - save the transient instance before merging: com.enterpriselayer.bo.Foo"
I want to avoid a get() to load the Foo instance from DB. How can i do that ?
Thanks