Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

@OnetoMany mappedBy behaviour - list not initialized - why

840589Feb 17 2011 — edited Feb 24 2011
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 none 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;
}
}

Edited by: 837586 on 17-Feb-2011 04:19
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2011
Added on Feb 17 2011
1 comment
236 views