Skip to main content

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

Hi Erhard,

by default an Entity with an Id of 0 or NULL means that it is a new object.

Therefore your code should persist a new object to the database.

You are using a SequenceGenerator therefore you should start with an Id > 0.

Otherwise check:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=277804

Please notice that allocationSize = 1 will result in pretty bad performance when persisting new objects.

Best regards,

Reinhard Girstenbrei

On 02/03/2017 06:00 PM, eclipselink-users-request@xxxxxxxxxxx wrote:
Send eclipselink-users mailing list submissions to
	eclipselink-users@xxxxxxxxxxx

To subscribe or unsubscribe via the World Wide Web, visit
	https://dev.eclipse.org/mailman/listinfo/eclipselink-users
or, via email, send a message with subject or body 'help' to
	eclipselink-users-request@xxxxxxxxxxx

You can reach the person managing the list at
	eclipselink-users-owner@xxxxxxxxxxx

When replying, please edit your Subject line so it is more specific
than "Re: Contents of eclipselink-users digest..."


Today's Topics:

    1. Data Loss when using getter on a lazy loaded	@OneToMany
       Relationship (Erhard Schwenk)


----------------------------------------------------------------------

Message: 1
Date: Fri, 3 Feb 2017 14:44:01 +0100
From: Erhard Schwenk <eschwenk@xxxxxx>
To: eclipselink-users@xxxxxxxxxxx
Subject: [eclipselink-users] Data Loss when using getter on a lazy
	loaded	@OneToMany Relationship
Message-ID: <21d8f63b-9c2e-cddc-58e6-3d27ca49e890@xxxxxx>
Content-Type: text/plain; charset=utf-8; format=flowed

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 */


}







Back to the top