-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to return a List with different criterion?
PostPosted: Thu Mar 23, 2006 7:20 pm 
Newbie

Joined: Thu Mar 23, 2006 5:35 am
Posts: 8
Hi,

I currently have a Java class which defines an order table and all order_lines (another table sorted by a sequence, and color) for that table. I use a Java List to return all of the order_lines rows and use the List index values as a reference. I need to make sure these index values remain constant even though new rows are added. There is also a status value on each order_line which is set to AVAILABLE if the line is new and IN_PROGRESS if the line has been processed. I would like to be able to return a list which contains only lines where the status is IN_PROGRESS. Some ideas I had are: (1) add a method in the Order Java class (not sure how to do this), (2) create an external query to return the list, or (3) change from List to another collection. What is the best way accomplish this?

Regards,
Rick

@Entity
@Table(name = "order_header")
public class Order {
private long id;
private List<OrderLine> lines = new ArrayList<OrderLine>();

@Id(generate = GeneratorType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "order_header_id")
@OrderBy("Sequence, Color")
public List<OrderLine> getLines() {
return lines;
}
public void setLines(List<OrderLine> lines) {
this.lines = lines;
}


// What is the best way to return List<OrderLine> as "select o from OrderLine where order_header_id = ? and status > AVAILABLE"?
// The elements returned in this List need to be modifiable.
...
}


@Entity
@Table(name = "order_line")
public class OrderLine {
private long id;
private String lineNo;

public enum Status {
AVAILABLE, IN_PROGRESS, SKIPPED }

private Status status = Status.AVAILABLE;

@Id(generate = GeneratorType.AUTO)
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

@Column(name = "line_no")
public String getLineNo() {
return lineNo;
}

public void setLineNo(String lineNo) {
this.lineNo = lineNo;
}

@Column(name = "status")
public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

...
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 12:23 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
Here are two ways:

write a method to filter the results from getLines:

Code:

getInProgressOrderLines() {
  return new ArrayList(CollectionUtils.select(getLines(), IN_PROGRESS_ORDERLINE_PREDICATE);
}



Alternatively, you can do it in the mapping, with a where clause, but this duplicates the mapping logic (which I am not a fan of) and you may have to return a set rather than a list (and then sort into a list) since the index column will not be contiguous.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 12:57 pm 
Newbie

Joined: Thu Mar 23, 2006 5:35 am
Posts: 8
Hi,

I tried using the following method:

@Transient
public List<PickingOrderLine> getLinesInProgress() {
return new ArrayList<PickingOrderLine>(CollectionUtils.select(getLines(), new InProgressPredicate()));
}

and the following predicate class:

public class InProgressPredicate implements Predicate {
public boolean evaluate(Object object) {
return true;
}
}

The result from getLinesInProgress() is an empty list (even though there are rows in getLines(). I earlier tried this routine and it also returned an empy list.

@Transient
public List<PickingOrderLine> getLinesInProgress() {
List<PickingOrderLine> l = new ArrayList<PickingOrderLine>();
for (int i = 0; i < getLines(); i++) {
l.add(getLines().get(i));
}
return l;
}

Regards,
Rick


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 1:05 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
what's getLines().size()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 2:20 pm 
Newbie

Joined: Thu Mar 23, 2006 5:35 am
Posts: 8
getLines().size() = 0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 2:29 pm 
Newbie

Joined: Thu Mar 23, 2006 5:35 am
Posts: 8
It seems that I replaced too may calls to getLines with getLinesInProgress and it was not inserting the records into the DB. So, now it seems to be working. Thanks!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.