openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From twelveeighty <>
Subject merge() doesn't refresh many-to-one members unless attribute is called explicitly?
Date Mon, 20 Jun 2011 02:17:54 GMT
Since this sounds like a basic operation that fails for me, I'm inclined to
think I'm doing something wrong, but I can't find anything similar in the
FAQ or online, so please tell me what I am missing here.

If I use merge() to insert a new entity that has a many-to-one relationship
to another entity, it gets created OK, but the *returned* object from
merge() has the related record defined with ONLY its primary key filled in,
all other attributes on the related record are null. To make it even
weirder: if I call "get<RelatedObject>()" while the object is still
attached, everything is fine.

Here are the details:

OpenJPA version 2.1.1

RelatedObject {
  private int relatedObjectId;

  @Column(nullable=false, length=255)
  private String shouldNeverBeNull;

ToBeMergedObject {
  private int toBeMergedObjectId;

// @ManyToOne(optional=false, cascade=CascadeType.REFRESH) - tried both,
didn't make a difference
  @JoinColumn(name="relatedObjectId", nullable=false, updatable=false)
  private RelatedObject relatedObject;

In the database, there is an existing row for RelatedObject with values (1,
'Some String'). An corresponding instance has been preloaded and has been
detached at some point, let's call this detached instance:

Now, create a new ToBeMergedObject (still outside an EntityManager's

ToBeMergedObject toBeMerged = new ToBeMergedObject();


CASE 1: Create a context and merge the new entity:

EntityManager em = [...]

ToBeMergedObject afterMerge = em.merge(toBeMerged);
if (afterMerge.getRelatedObject().getShouldNeverBeNull()==null) {
  log.error("HOW IS THIS POSSIBLE?");

CASE 2: Create a context and merge the new entity, but make a (useless) call
to getRelatedObject() before the em.close();

EntityManager em = [...]

ToBeMergedObject afterMerge = em.merge(toBeMerged);
logger.debug("Some useless call: " +
if (afterMerge.getRelatedObject().getShouldNeverBeNull()==null) {
  log("doesn't happen");
} else {
  log("NOW IT IS AS EXPECTED - getShouldNeverBeNull() returns a value");

As I mention in the definition of the many-to-one pseudo code, I have tried
with cascade=CascadeType.REFRESH but that makes no difference.

Why is it that with a call to the getShouldNeverBeNull() INSIDE the "scope"
of the EntitiyManager that the object gets returned "fully loaded", but
without it, it doesn't?

I appreciate any help you folks can give me on this one. 

View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message