Mapping:
Code:
<hibernate-mapping schema="ECM_USER2">
<class name="com.mycompany.ecm.query.dto.ECMUserBrowserDetailDTO"
table="ECM_USR_BRWS_DTL"
dynamic-update="true"
dynamic-insert="true">
<composite-id>
<key-property name="userId" type="string" length="7" column="EMP_CORP_ID" />
<key-property name="screenName" type="string" length="25" column="ECM_SCRN_NM" />
</composite-id>
<property name="beginDate" type="java.util.Date" column="BEG_DT" />
<property name="browserWidth" type="java.lang.Integer" column="BRWS_WND_WTH_VLU" not-null="true"/>
<property name="browserHeight" type="java.lang.Integer" column="BRWS_WND_HT_VLU" not-null="true"/>
<property name="browserXCoordinate" type="java.lang.Integer" column="BRWS_X_CORD_VLU" not-null="true"/>
<property name="browserYCoordinate" type="java.lang.Integer" column="BRWS_Y_CORD_VLU" not-null="true"/>
</class>
</hibernate-mapping>
Code (first findUserBrowserDetail relates to the sql posted in original post). Each of the 3 sql statements are all from calls to the same findUserBrowserDetail method, but the AS 'generated column' gets tacked on, thus preventing prepared statements from being cached.
Code:
package com.mycompany.ecm.query.dao;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import com.mycompany.ecm.query.dto.ECMUserBrowserDetailDTO;
import com.mycompany.ecm.query.util.ServiceConstants;
/**
*
* Implementation class to perform CRUD operation with ECM Query
* Database table - ECM User Security using hiberante(O-R mapping table)
*
*/
public class ECMUserBrowserDetailDAOImpl implements ECMUserBrowserDetailDAO,ServiceConstants {
/**
* Represents Hibernate session factory
*/
private SessionFactory sessionFactory;
static Logger logger = Logger.getLogger(ECMUserBrowserDetailDAOImpl.class);
/**
* default constructor
*/
public ECMUserBrowserDetailDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Obtains the current session.
*
* @return the transactional session
*/
protected Session getCurrentSession() {
return sessionFactory.openSession();
}
/**
* Insert an record in ECM Query database table ECM User Browser Detail.
*
* @param browserDetail
* @return ECMUserBrowserDetailDTO
*/
public ECMUserBrowserDetailDTO addUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
getCurrentSession().save(browserDetail);
return browserDetail;
}
/**
* Update the given record in ECM Query database table ECM User Browser Detail.
*
* @param browserDetail
*/
public void updateUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
getCurrentSession().update(browserDetail);
}
/**
* Update the given record in ECM Query database table ECM User Browser Detail
* If the record not exist in the Db inserts a new record.
*
* @param browserDetail
*/
public void saveOrUpdateUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
Session session = null;
try{
session = getCurrentSession();
session.saveOrUpdate(browserDetail);
}
catch(Exception e){
logger.error("Exception occured while saving user preferences "+e);
}
finally{
if(session != null){
session.flush();
session.close();
}
}
}
/**
* Delete the given record in ECM Query database table User Browser Detail.
*
* @param userSecurity
*/
public void deleteUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
getCurrentSession().delete(browserDetail);
}
/**
* Get and return the list of browser settings for the given screen(s).
*
* @param screenName
* @param userId
* @return List
*/
public List findUserBrowserDetails(String userId,String screenName)
{
List browserDetailsList = null;
Session session = null;
try{
String[] preferences = {userId,USER_ID_DEFAULT};
session = getCurrentSession();
Query query =
session.createQuery("from ECMUserBrowserDetailDTO " +
"browserDetail where " +
"browserDetail.screenName = ? and browserDetail.userId in (:preferences)");
query.setString(0, screenName);
query.setParameterList("preferences", preferences);
browserDetailsList = query.list();
}
catch(Exception e){
logger.error("Exception occured while getting user preferences "+e);
}
finally{
if(session != null){
session.flush();
session.close();
}
}
return browserDetailsList;
}
/**
*
* Get and return the browser detail for the given input.
*
* @param userSecurity
* @return List
*/
public List findUserBrowserDetail(ECMUserBrowserDetailDTO browserDetail) {
Criteria crit = null;
try{
crit = getCurrentSession().createCriteria(ECMUserBrowserDetailDTO.class);
}
catch(Exception e){
logger.error("Exception occured while finding user preferences "+e);
}
if(browserDetail!=null){
if(browserDetail.getScreenName()!=null){
crit.add(Restrictions.eq("screenName",browserDetail.getScreenName()));
}
if(browserDetail.getBeginDate()!=null){
crit.add(Restrictions.eq("beginDate",browserDetail.getBeginDate()));
}
if(browserDetail.getUserId()!=null){
crit.add(Restrictions.eq("userId",browserDetail.getUserId()));
}
}
return crit.list();
}
/**
* Get and return the list of security level exist in the
* ECM Query database table ECM User Browser Detail.
*
* @return List
*/
public List findAllUserBrowserDetail() {
return getCurrentSession()
.createQuery(
"from ECMUserBrowserDetailDTO browserDetail")
.list();
}
}