Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Query to filter collection
Query to filter collection [message #809095] Tue, 28 February 2012 13:29 Go to next message
Pavel Zelenka is currently offline Pavel Zelenka
Messages: 61
Registered: July 2009
Member
Hi,

I would appreciate any advice with this problem:

Let's assume I have two entities: Order and Item (one order can contain number of items)

@Entity
public class Order
@Id
@Column(name = "order_id")
private Integer orderId;
@Column(name = "order_date")
@Temporal(TemporalType.DATE)
private Date orderDate
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
private List<Item> itemList
....

@Entity
public class Item
@Id
@Column(name = "item_id")
private Integer itemId;
@Column(name="item_text")
private String itemText;
@Column(name="item_value")
private Integer itemValue;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "order_id", referencedColumnName = "order_id")
private Order order
....

I want to select orders which contains items where item value=100. But I want to Order.itemList to be filled only with items of value 100.

Is this possible (EclipseLink 1.1.4, Tomcat 6)

Thanks
Re: Query to filter collection [message #809320 is a reply to message #809095] Tue, 28 February 2012 18:38 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1035
Registered: July 2009
Senior Member
Not with JPA. JPA requires that entities returned from the EntityManager be managed, and so should reflect the data as it exists in the database. Only partially populating the itemList collection can leave the entity corrupt in the cache - on the next query when you want all items you will still only see items in the itemList that met your filter criteria.

It can be done, but I wouldn't recommend adding filter logic to the mapppings that the application can easily do itself when required. For instance, you can easily query for all items with value=100 and join it to Order so you have a list of items so itemList doesn't need to be accessed. ie "Select item From Item item Fetch Join item.order Where item.value=100".

Regards,
Chris
Re: Query to filter collection [message #809775 is a reply to message #809320] Wed, 29 February 2012 08:47 Go to previous message
Pavel Zelenka is currently offline Pavel Zelenka
Messages: 61
Registered: July 2009
Member
I expected that reply. Anyway big thanks.

[Updated on: Wed, 29 February 2012 08:47]

Report message to a moderator

Previous Topic:NOT NULL and NOT NULL FK constraints
Next Topic:JPQL: SIZE function causing strange exceptions
Goto Forum:
  


Current Time: Fri Oct 31 13:07:20 GMT 2014

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

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