开发者

Hibernate Help required

I tried to look around for information regarding returning a list using Hibnerate but couldn't find something that matches what I'm looking for and thus needs some advise.

I have the following classes

BPDataPK:

  public class BPDataPK implements Serializable {
    private String id;
    private int userProfile;
    private Date when;      
         .....
  }

BPData:

public class BPData implements Serializable {   
   private BPDataPK dataPK;
   private Date sessionStart;
    ...
}

HibernateBPDataDAO :

public class HibernateBPDataDAO extends Hi开发者_C百科bernateDaoSupport implements IBPDataDAO{
    ....
public BPData[] getSessionBPData(Session session) throws Exception {
        /**I need to get a list of BPData that matches the following
        1.  BPDataPK.id== session.getID;
        2.  BPDataPK.userProfile == session.getUserProfile;
        **3.  BPData.sessionStart  == session.getSessionStart();**
        */
    }
 }

How do I return a list of BPData that matches two of the primary key and a non primary key??


You should not look around for some information. Instead, you should read the reference documentation.

This documentation has a full chapter dedicated to queries using the HQL query language, and another one dedicated to criteria queries. Since you have a fixed set of criteria here, HQL is more suited to the task:

String hql = "select b from BPData b where b.dataPK.id = :id"
             + " and b.dataPK.userProfile = :profile"
             + " and b.sessionStart = :sessionStart";
Query q = hibernateSession.createQuery(hql);
q.setString("id", session.getID());
q.setInt("profile", session.getUserProfile());
q.setTimestamp("sessionStart", session.getSessionStart()); // or setDate, depending on the type of this field
List<BPData> result = q.list();


if you are using JPA immplementaion of hibernate you do this

@Entity
@NamedQuery(
    name="findByMyQuery",
    queryString="SELECT bpdata FROM BPData bpdata WHERE bpdata.dataPK = :sessionId AND bpdata.userProfile = :userProfile AND bpdata.sessionStart = :sessionStart "
)
public class BPData implements Serializable {

    private BPDataPK dataPK;
    private Date sessionStart;
        ...
    }


public BPData[] getSessionBPData(Session session) throws Exception {       

    Query queryFindByMyQuery = entityManager.createNamedQuery("findByMyQuery");
    queryEmployeeByFirstName.setParameter("sessionId", session.getID());
    queryEmployeeByFirstName.setParameter("userProfile", session.getUserProfile());
    queryEmployeeByFirstName.setParameter("sessionStart", session.getSessionStart());
    return queryEmployessByFirstName.getResultList().toArray();
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜