Hi there!
I have a Many-To-One Relation between the entities AdLog and Page. In the AdLog entity there should be inserted an entry each time a Page gets displayed. And a Page should only be displayed when its adMaxTime is not overstepped. So, I came to the conclusion that I have to join the entities, sum up AdLog.duration and compare the sum with Page.adMaxTime. Finally I should get a List of Pages.
I tried several things with createCriteria, setProjections, createAlias and setFetchMode. Nothing worked for me. Maybe s.o. can help me with this as I do not find any helpful documentation/example of how to handle this issue.
Thanks in advance!
Mapping documents:
Code:
@Entity()
@Table(name = "page")
public class Page implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "pag_id", nullable = false)
private int id;
@OneToMany(mappedBy="page")
private List<AdAllocation> adAllocations = new ArrayList<AdAllocation>();
@OneToMany(mappedBy="page")
private List<AdLog> adLogs = new ArrayList<AdLog>();
@Column(name = "pag_admaxtime", nullable = true)
private Integer adMaxTime = null;
@Column(name = "pag_title", nullable = false, length = 128)
private String title;
Code:
@Entity()
@Table(name = "adlog")
public class AdLog implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "adl_id", nullable = false)
private int id;
@ManyToOne(optional = false)
@JoinColumn(name = "pag_id", nullable = false)
private Page page;
@Column(name = "adl_duration", nullable = false)
private int duration = 30;
@Column(name = "adl_datetime", nullable = false)
private Date datetime = new Date(0);