Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @OneToMany MappedBy behaviour - list not initialized
@OneToMany MappedBy behaviour - list not initialized [message #654850] Thu, 17 February 2011 12:39 Go to next message
jj Missing name is currently offline jj Missing name
Messages: 9
Registered: February 2011
Junior Member
I am having difficulty following the basic one-to-many mappedBy behaviour. Basically an entity retrieval where mappedBy is used does not return an initilized list in the owning entity. Whilst if no mappedBy is used the collection is initilized.

core unit test:
// setup - populate & persist an order with 1 item.
// test..
Order order= (Order) entityManager.find(Order.class, validOrderID);
assertEquals(1, order.getItems().size());

Order contains a collection of Items, I would expect retrieving the Order in both cases would bring back an Order instance with items containing a list with 0 or more elements, but never null. practically if mappedBy is used then items is null, if mappedBy is not used the list is as expected - What am I misunderstanding?

@Entity
public class Item {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToOne
private Order order;


public Order getOrder() {
return order;
}
public Long getId() {
return id;
}
}

@Entity
@Table(name="T_ORDER")
public class Order {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@OneToMany(mappedBy = "order", cascade=CascadeType.ALL)
//@OneToMany( cascade=CascadeType.ALL)
@JoinColumn(name="ORDER_ID")
private Collection<Item> items = new LinkedHashSet<Item>();


public Collection<Item> getItems() {
return items;
}
public void setItems(Collection<Item> items) {
this.items = items;
}
public Long getId() {
return id;
}
}

(EclipseLink, within STS, + embedded db & DB2)
Re: @OneToMany MappedBy behaviour - list not initialized [message #654863 is a reply to message #654850] Thu, 17 February 2011 13:27 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1021
Registered: July 2009
Senior Member
Hello,

Please turn logging on, as the combination of @JoinColumn on a @OneToMany using mappedBy = "order" should be indicating a problem. They are not compatible - can you try your test commenting out the @JoinColumn when using the mappedby as well?

Best Regards,
Chris
Re: @OneToMany MappedBy behaviour - list not initialized [message #654932 is a reply to message #654863] Thu, 17 February 2011 16:34 Go to previous messageGo to next message
jj Missing name is currently offline jj Missing name
Messages: 9
Registered: February 2011
Junior Member
Thanks Chris for quick reply, your suggestion worked, it also focused me on another problem - Item was missing a setOrder(item) method.

The test case now works Smile, with or without the joinColumn

Order order = new Order();
Item item = new Item();
item.setProduct("foo");

order.getItems().add(item);
item.setOrder(order);

entityManager.persist(order);
entityManager.flush();
entityManager.clear();

Order other = (Order) entityManager
.createQuery("select o from Order o join o.items i where i.product=:product").setParameter("product",
"foo").getSingleResult();
assertEquals(1, other.getItems().size());

----

I notice I have to update both sides on the relationship i.e. add item to order's list & add order to item - is this generally the norm? I would have thought updating one side should be sufficent

order.getItems().add(item);
item.setOrder(order);

jj
Re: @OneToMany MappedBy behaviour - list not initialized [message #654971 is a reply to message #654932] Thu, 17 February 2011 19:59 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1021
Registered: July 2009
Senior Member
Hello,

The database relationship is controlled by the 'owning' side when the entities have a bidirectional relationship. So you can update only one side, but the java objects will not reflect the change on the other side until they are refreshed. Entities are just plain java objects cached within the EntityManager. So its best practice to update both sides so that your cache is in synch with what is in the database. Same thing goes for deleting an object - the application should null out any references to it or there will be inconsistencies in the cache.

Best Regards,
Chris
Previous Topic:double record in history
Next Topic:Running CanonicalModelProcessor in maven causes 100% CPU usage
Goto Forum:
  


Current Time: Thu Sep 18 14:00:05 GMT 2014

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

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