Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Null value if read the same @ManyToOne Entity
Null value if read the same @ManyToOne Entity [message #1062374] Fri, 07 June 2013 12:46 Go to next message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
Hi,
i use EclipseLink 2.4, JPA 2.0 and Spring framework.
I have the following entity:

@Entity
@Table(name="DIPENDENTE")
public class Dipendente implements Serializable {

@Id
@Column(name="C_DIPENDENTE")
private String cDipendente;

@Column(name="NOME")
private String nome;

@Column(name="COGNOME")
private String cognome;

@ManyToOne
@JoinColumn(name="RECAPITO")
private Recapito recapito;

.......
}

@Entity
@Table(name="RECAPITO")
public class Recapito implements Serializable {

@Id
@Column(name="C_RECAPITO")
private String cRecapito;

@Column(name="VIA")
private String via;

@Column(name="LANG")
private String lang;
.......

}

after Dipendente entity persist i have the following situation in db:
C_DIPENDENTE NOME COGNOME RECAPITO
12345 Mario Rossi 1
67890 Luca Verdi 1

C_RECAPITO VIA LANG
1 Via Manzoni IT

But when i read all data with the following JPQL query:

query.append("SELECT d FROM Dipendente d ");
query.append("JOIN FETCH d.recapito r ");
query.append("WHERE ");
query.append("d.lang = 'IT' ");

the field VIA of Recapito entity is set to null value. I don't know why.
Any ideas?
Thanks

Re: Null value if read the same @ManyToOne Entity [message #1062382 is a reply to message #1062374] Fri, 07 June 2013 13:11 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
How are you persisting the Dipendente and associating the Recapito? Make sure that you are reading in the Recapito if it exists by using either a query, find or getReference call rather than creating an empty instance with only the pk. Otherwise the newly created Dipendente will reference the empty Recapito when retrieved until it gets refreshed.


Re: Null value if read the same @ManyToOne Entity [message #1062422 is a reply to message #1062382] Fri, 07 June 2013 15:19 Go to previous messageGo to next message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
Method is like the following:

Recapito r = new Recapito();
r.setCRecapito("1");
r.setLang("IT");

Dipendente d = new Dipendente();
d.setCDipendente("12345");
d.setNome("Mario");
d.setCognome("Rossi");
d.setRecapito(r);

...

try {
entityManager.persist(gpfProvider);
entityManager.flush();
} catch (Exception e) {
e.printStackTrace();
}

Query query = entityManager.createQuery("SELECT d FROM Dipendente d JOIN FETCH d.recapito recapito WHERE recapito.lang = 'IT'");

List<Dipendente> result = (Vector<Dipendente>) query.getResultList();
Re: Null value if read the same @ManyToOne Entity [message #1062631 is a reply to message #1062382] Mon, 10 June 2013 08:06 Go to previous messageGo to next message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
Yes, this is the problem Chris.

I change in:

Recapito r = entityManager.find(Recapito.class, r.getCRecapito);

Dipendente d = new Dipendente();
d.setCDipendente("12345");
d.setNome("Mario");
d.setCognome("Rossi");
d.setRecapito(r);

try {
entityManager.persist(gpfProvider);
entityManager.flush();
} catch (Exception e) {
e.printStackTrace();
}

It work fine.
But..why in another cases work fine not use find operation?
Re: Null value if read the same @ManyToOne Entity [message #1062728 is a reply to message #1062631] Mon, 10 June 2013 14:28 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
It does not read it in because it is perfectly valid to use an empty Recapito to avoid having to read it in if only to set the foreign key. The JPA specification doesn't cover what to do on persist to non-managed entities without cascade persist, but it does for merge, and the preference is to pass back managed instances which could be why it is using the populated Recapito if it was previously read in. You can verify by calling em.contains(d.getRecapito()); in the first method you posted right after the persist call to see if it is making the empty Recapito managed or if it is just keeping the reference as is.

Best Regards,
Chris
Re: Null value if read the same @ManyToOne Entity [message #1062804 is a reply to message #1062728] Mon, 10 June 2013 19:32 Go to previous messageGo to next message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
Ok, I understand...so, if I have:

@Entity
@Table(name="DIPENDENTE")
public class Dipendente implements Serializable {

@Id
@Column(name="C_DIPENDENTE")
private String cDipendente;

@Column(name="NOME")
private String nome;

@Column(name="COGNOME")
private String cognome;

@OneToMany(mappedBy="dipendente")
private List<Recapito> recapito;

.......
}

@Entity
@Table(name="RECAPITO")
public class Recapito implements Serializable {

@Id
@Column(name="C_RECAPITO")
private String cRecapito;

@Column(name="VIA")
private String via;

@Column(name="LANG")
private String lang;

@ManyToOne
private Dipendente dipendente
.......

}

the instruction
Dipendente d = entityManager.find(Dipendente.class, d.getCDipendente);
read Dipendente instance with all his Recapito list. So, if I want to use merge operation for one of this, I proceed in this way:

for (Recapito r : d.getRecapito()) {
if("IT".equals(r.getLang)){
r.setVia("TEST");
}
}
entityManager.merge(d);

Is this the right way?

Thank you so much Chris
Re: Null value if read the same @ManyToOne Entity [message #1062864 is a reply to message #1062804] Tue, 11 June 2013 07:41 Go to previous message
F C is currently offline F CFriend
Messages: 12
Registered: June 2013
Junior Member
Ok, I added
cascade = {CascadeType.PERSIST,CascadeType.MERGE}
for merge operation.
Smile
Previous Topic:Determine 'create' or 'update' Entity
Next Topic:JMS cache coordination on GlassFish cluster
Goto Forum:
  


Current Time: Thu Mar 28 13:43:01 GMT 2024

Powered by FUDForum. Page generated in 0.05940 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top