Hibernate in Java Question
Hello Everyone :)
I'm hoping you can help, I've been stuck on a project that uses
Hibernate to persist the objects in it, unfortunately the peice of
functionality i have to implement is to pull two Objects out of the
Database which are linked in a one to one relationship.
The project uses Annotations which i need to stick with yet all the
books seem to try and teach you in hbm files. Since the rest of the
hibernate code needs to only pull out a single object i have nothing
to refer to and its driving me mad trying to figure out what i need to
do.
What i'm trying to do corresponds to the sql statement:
SELECT sum(sessions), sum(searches), sum(turnaways) FROM counted,
product_session WHERE counted.session_id=product_session.session_id;
Here are my objects:
@Entity
@Table(name="product_session")
public class ProductSession implements Serializable {
private static final long serialVersionUID =
638116842380420385L;
public ProductSession(int session_id, String userid, String
date,
BSCCountedItem item)
{
this.session_id = session_id;
this.userid = userid;
this.date = date;
this.item = item;
}
public ProductSession()
{ }
@Id
private int session_id;
@Column
private String userid;
@Column
private String date;
@OneToOne
@JoinColumn (name = "session_id")
public CountedItem item;
@SuppressWarnings("deprecation")
public static ProductSession[] getProductSessions(Session
session,
CounterDate date, String userid)
{
Query query = session.createQuery("SELECT
ProductSession" +
"FROM ProductSession "+
"WHERE date <= :enddate AND date >= :startdate AND userid
= :userid");
query.setDate("startdate", date.getStartDate());
query.setDate("enddate", date.getEndDate());
query.setString("userid", userid);
return (ProductSession[]) query.list().toArray();
}
// Getters and Setters here
}
@Entity
@Table(name="counted")
public class CountedItem implements Serializable {
private static final long serialVersionUID =
-3473918695418644968L;
public CountedItem(int session_id, int sessions, int searches,
int
turnaways) { }
@Id
@GeneratedValue
@Column
private int session_id;
@Column
private int sessions;
@Column
private int searches;
@Column
private int turnaways;
// GETTERS & SETTERS //
}
Can you please tell me where i'm going wrong - i've had a really good
go at this and i'm still completely lost as to where i'm not doing
something right!
Thanks for any help!
Graeme