View Javadoc
1   package myorg.relex.collection;
2   
3   import java.util.Collections;
4   import java.util.LinkedList;
5   import java.util.List;
6   
7   import javax.persistence.*;
8   
9   /**
10   * This entity class provides an example of an ordered list of child entities ordered by a business property
11   * in the child entity.
12   */
13  @Entity
14  @Table(name="RELATIONEX_PATH")
15  public class Path {
16  	@Id @GeneratedValue
17  	private int id;
18  	
19  	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
20  	@JoinColumn
21  	@OrderBy("number ASC")
22  	private List<Segment> segments;
23  	
24  	@Column(length=16)
25  	private String name;
26  
27  	public int getId() { return id; }
28  
29  	public List<Segment> getSegments() {
30  		if (segments==null) { segments = new LinkedList<Segment>(); }
31  		return segments; 
32  	}
33  	//private class SegmentComparator implements Comparator<Segment>
34  	
35  	public Path addSegment(Segment segment) {
36  		getSegments().add(segment);
37  		/*
38  		Collections.sort(segments, new Comparator<Segment>() {
39  			@Override
40  			public int compare(Segment lhs, Segment rhs) {
41  				if (lhs == rhs || lhs==null && rhs == null) { return 0; }
42  				if (lhs != null && rhs == null) { return 1; }
43  				if (lhs == null && rhs != null) { return -1; }
44  				int result = lhs.getNumber() - rhs.getNumber();
45  				log.debug(lhs.getClass().getSimpleName() + lhs.toString() + 
46  						".compareTo" + rhs.toString() + 
47  						"=" + result
48  						);
49  				return result;
50  			}});
51  			*/
52  		Collections.sort(segments);
53  		return this;
54  	}
55  
56  	public String getName() { return name; }
57  	public void setName(String name) {
58  		this.name = name;
59  	}
60  }