Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Data Loss when using getter on a lazy loaded @OneToMany Relationship

Hello,

I have a Problem with a @OneToMany Relationship.

Environment:
Java SE, no Weaving (no javaagent started), Eclipselink 2.5.2 (the one shipped with NetBeans IDE 8.1)

Now I have the following Code sequence:

Master master = em.find(Master.class, 0);
master.setLastChange(new Date());
em.getTransaction().begin();
List<Detail> someDetails = master.getDetails();
if (!em.contains(master)) {
	master = (Master) em.merge(master);
}
em.flush();
em.getTransaction().commit();

And reproducable, Master.lastChange keeps null on Database. In Debugger, the lastChange value vanishes on the master.getDetails() Call (looks like it gets replaced by the value previously in the database).

According to my knowledge, even changes before the Start of the Transaction should be written to Database on flush/commit.

What is wrong here and how can I get it to work?

My Entity Beans (a little bit simplified) are:

@Entity
@Table(name = "MASTER")
public class Master implements Serializable {

	public static final long serialVersionUID = 1L;

	@Id
	@Basic(optional = false)
@SequenceGenerator(name = "MASTER_SEQ", sequenceName ="MASTER_SEQ", allocationSize = 1) @GeneratedValue(generator = "MASTER_SEQ", strategy = GenerationType.SEQUENCE)
	@Column(name = "ID", nullable = false)
	private Long id;

	@Column(name = "LASTCHANGE")
	@TEmporal(TemporalType.DATE)
	private Date lastChange;

@OneToMany(mappedBy = "master", cascade = CascadeType.ALL, orphanRemoval = ture, fetch = FetchType.LAZY)
	private List<Detail> details;


/* standards setters and getters for all Attributes; id has no setter; just one example to clarify: */

	public Date getLastChange() {
		return lastChange;
	}

	public void setLastChange(Date lastChange) {
		this.lastChange = lastChange;
	}

}


	
@Entity
@Table(name = "DETAIL")
public class Detail implements Serializable {

	public static final long serialVersionUID = 1L;

	@EmbeddedId
	private DetailPK detailPK;

	@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "MASTER_ID", referencedColumnName = "ID", nullable = false, insertable = true, updatable = true)
	Master master;

	@Column(name = "CONTENT")
	private String content;

/* standard Setters and getters also here */

}

@Embeddable
public class DetailPK implements Serializable {

	public static final long SerialVersionUID = 1L;

	@Basic(optional = false)
@Column(name = "MASTER_ID", nullable = false, updatable = false, insertable = false)
	private Long masterId;

	@Basic(optional = false)
	@Column(name = "DETAIL_ID")
	private Long detailId;

/* and again, we have standard setters and getters */


}




--
Erhard Schwenk

Akkordeonjugend Baden-Württemberg - http://www.akkordeonjugend.de/
APAYA running System - http://www.apaya.net/


Back to the top