Re: Data Loss when using getter on a lazy, loaded @OneToMany Relationship

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Data Loss when using getter on a lazy, loaded @OneToMany Relationship

Reinhard Girstenbrei
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, [hidden email] wrote:

> Send eclipselink-users mailing list submissions to
> [hidden email]
>
> 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
> [hidden email]
>
> You can reach the person managing the list at
> [hidden email]
>
> 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 <[hidden email]>
> To: [hidden email]
> Subject: [eclipselink-users] Data Loss when using getter on a lazy
> loaded @OneToMany Relationship
> Message-ID: <[hidden email]>
> 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 */
>
>
> }
>
>
>
>

_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
Reply | Threaded
Open this post in threaded view
|

Re: Data Loss when using getter on a lazy, loaded @OneToMany Relationship

eschwenk
Am 09.02.2017 um 12:13 schrieb [hidden email]:
> 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.
>

definitely not. It is using an existing one. Cite from my post:

 >> Master master = em.find(Master.class, 0);


--
Erhard Schwenk

Akkordeonjugend Baden-W├╝rttemberg - http://www.akkordeonjugend.de/
APAYA running System - http://www.apaya.net/
_______________________________________________
eclipselink-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users