OK, I figured it out after lots of digging online. There are two ways of doing this that I now know of. One using the @Where annotation and the other using the @FilterDef/@Filter annotations. I find the @Filter annotations to be the more elegant solution as I can program in the number of latest entries I want returned. The first code segment will be how to do this with the @Where annotation and the second will be with the @Filter annotations:
Code:
@Entity
@Table( name="person")
public class Person
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="personid_gen")
@SequenceGenerator(name="personid_gen",sequenceName="personid_seq",
allocationSize=1 )
@Column(name="person_id", nullable=false)
public Integer getID() { return this.id; }
public void setID( Integer id ) { this.id = id; }
@Column( name="name", length=50 )
public String getName() { return this.name; }
public void setName( final String name ) { this.name = name; }
@OneToMany(mappedBy="person", targetEntity=Position.class,
fetch=FetchType.EAGER, cascade = CascadeType.ALL)
@Where(clause="postion_id = (select max(p.postion_id) from position p where p.person_id = person_id)")
public List<Position> getDetections() { return this.positions; }
public void setDetections( List<Position> positions )
{
this.positions = positions;
}
private Integer id;
private String name;
private List<Position> positions = new ArrayList<Position>();
}
@Entity
@Table( name="position")
public class Position
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="positionid_gen")
@SequenceGenerator(name="positionid_gen",sequenceName="positionid_seq",
allocationSize=1 )
@Column(name="position_id", nullable=false)
public Integer getID() { return this.id; }
public void setID( Integer id ) { this.id = id; }
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="person_id", nullable=false)
public Person getPerson() { return this.person; }
public void setPerson( Person person ) { this.person = person; }
@Column( name="time_recorded")
public Long getUpdateTime() { return this.time; }
public void setUpdateTime( final Long time ) { this.time = time; }
@Column( name="latitude")
public Float getLatitude() { return latitude; }
public void setLatitude( final Float latitude )
{
this.latitude = latitude;
}
@Column( name="longitude")
public Float getLongitude() { return longitude; }
public void setLongitude( final Float longitude )
{
this.longitude = longitude;
}
private Integer id;
private Long time;
private Float latitude;
private Float longitude;
private Person person;
}
Here it is using the @Filter annotations:
Code:
@Entity
@FilterDef(name="numRecordsFilter", parameters=@ParamDef( name="numRecords", type="integer" ) )
@Table( name="person")
public class Person
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="personid_gen")
@SequenceGenerator(name="personid_gen",sequenceName="personid_seq",
allocationSize=1 )
@Column(name="person_id", nullable=false)
public Integer getID() { return this.id; }
public void setID( Integer id ) { this.id = id; }
@Column( name="name", length=50 )
public String getName() { return this.name; }
public void setName( final String name ) { this.name = name; }
@OneToMany(mappedBy="person", targetEntity=Position.class,
fetch=FetchType.EAGER, cascade = CascadeType.ALL)
@Filter(name="numRecordsFilter", condition="postion_id > (select max(p.postion_id) from position p where p.person_id = person_id) - :numRecords")
@Where(clause="postion_id = (select max(p.postion_id) from position p where p.person_id = person_id)")
public List<Position> getDetections() { return this.positions; }
public void setDetections( List<Position> positions )
{
this.positions = positions;
}
private Integer id;
private String name;
private List<Position> positions = new ArrayList<Position>();
}
@Entity
@Table( name="position")
public class Position
{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="positionid_gen")
@SequenceGenerator(name="positionid_gen",sequenceName="positionid_seq",
allocationSize=1 )
@Column(name="position_id", nullable=false)
public Integer getID() { return this.id; }
public void setID( Integer id ) { this.id = id; }
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="person_id", nullable=false)
public Person getPerson() { return this.person; }
public void setPerson( Person person ) { this.person = person; }
@Column( name="time_recorded")
public Long getUpdateTime() { return this.time; }
public void setUpdateTime( final Long time ) { this.time = time; }
@Column( name="latitude")
public Float getLatitude() { return latitude; }
public void setLatitude( final Float latitude )
{
this.latitude = latitude;
}
@Column( name="longitude")
public Float getLongitude() { return longitude; }
public void setLongitude( final Float longitude )
{
this.longitude = longitude;
}
private Integer id;
private Long time;
private Float latitude;
private Float longitude;
private Person person;
}