342 lines
11 KiB
Plaintext
342 lines
11 KiB
Plaintext
<#if c2h.getClassName(clazz).indexOf("Ccrm")!=-1>
|
|
package mnp.database.hb.dao.ccrm;
|
|
<#else>
|
|
package mnp.database.hb.dao;
|
|
</#if>
|
|
|
|
|
|
// Generated ${date} by Hibernate Tools Customizzato per MNP da C.A. ${version}
|
|
|
|
<#assign classbody>
|
|
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>/**
|
|
* DAO object for domain model class ${declarationName}.
|
|
* @see ${pojo.getQualifiedDeclarationName()}
|
|
* @author C.A.
|
|
*/
|
|
<#if ejb3>
|
|
@${pojo.importType("javax.ejb.Stateless")}
|
|
</#if>
|
|
import org.hibernate.Query;
|
|
import org.hibernate.criterion.Criterion;
|
|
import org.hibernate.Criteria;
|
|
import java.util.*;
|
|
<#if c2h.getClassName(clazz).indexOf("Ccrm")!=-1>
|
|
import mnp.database.hb.dao.BaseHibernateDAO;
|
|
import mnp.database.hb.dto.ccrm.*;
|
|
<#else>
|
|
import mnp.database.hb.dto.*;
|
|
</#if>
|
|
|
|
public class ${declarationName}DAO extends BaseHibernateDAO{
|
|
|
|
protected static final ${pojo.importType("org.apache.commons.logging.Log")} log = ${pojo.importType("org.apache.commons.logging.LogFactory")}.getLog(${pojo.getDeclarationName()}DAO.class);
|
|
|
|
<#if ejb3>
|
|
@${pojo.importType("javax.persistence.PersistenceContext")} private ${pojo.importType("javax.persistence.EntityManager")} entityManager;
|
|
|
|
public void persist(${declarationName} transientInstance) {
|
|
log.debug("persisting ${declarationName} instance");
|
|
try {
|
|
entityManager.persist(transientInstance);
|
|
log.debug("persist successful");
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("persist failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public void remove(${declarationName} persistentInstance) {
|
|
log.debug("removing ${declarationName} instance");
|
|
try {
|
|
entityManager.remove(persistentInstance);
|
|
log.debug("remove successful");
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("remove failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public ${declarationName} merge(${declarationName} detachedInstance) {
|
|
log.debug("merging ${declarationName} instance");
|
|
try {
|
|
${declarationName} result = entityManager.merge(detachedInstance);
|
|
log.debug("merge successful");
|
|
return result;
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("merge failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
<#if clazz.identifierProperty?has_content>
|
|
public ${declarationName} findById( ${pojo.getJavaTypeName(clazz.identifierProperty, jdk5)} id) {
|
|
log.debug("getting ${declarationName} instance with id: " + id);
|
|
try {
|
|
${declarationName} instance = entityManager.find(${pojo.getDeclarationName()}.class, id);
|
|
log.debug("get successful");
|
|
return instance;
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("get failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
</#if>
|
|
<#else>
|
|
|
|
public void save(${declarationName} transientInstance) {
|
|
log.debug("saving ${declarationName} instance");
|
|
try {
|
|
getSession().save(transientInstance);
|
|
log.debug("save successful");
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("save failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public ${pojo.importType("java.util.List")} findByProperty(String propertyName, Object value) {
|
|
log.debug("finding ${declarationName} instance with property: " + propertyName
|
|
+ ", value: " + value);
|
|
try {
|
|
String queryString = "select model from ${declarationName} model where model."
|
|
+ propertyName + "= ?";
|
|
Query queryObject = getSession().createQuery(queryString);
|
|
queryObject.setParameter(0, value);
|
|
return queryObject.list();
|
|
} catch (RuntimeException re) {
|
|
log.error("find by property name failed", re);
|
|
throw re;
|
|
}
|
|
finally {
|
|
closeSession();
|
|
}
|
|
}
|
|
|
|
public ${pojo.importType("java.util.List")} findAll() {
|
|
log.debug("finding all ${declarationName} ");
|
|
try {
|
|
String queryString = " from ${declarationName}";
|
|
Query queryObject = getSession().createQuery(queryString);
|
|
return queryObject.list();
|
|
} catch (RuntimeException re) {
|
|
log.error("find all name failed", re);
|
|
throw re;
|
|
}finally {
|
|
closeSession();
|
|
}
|
|
}
|
|
|
|
|
|
public void attachDirty(${declarationName} instance) {
|
|
log.debug("attaching dirty ${declarationName} instance");
|
|
try {
|
|
getSession().saveOrUpdate(instance);
|
|
log.debug("attach successful");
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("attach failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public void attachClean(${declarationName} instance) {
|
|
log.debug("attaching clean ${declarationName} instance");
|
|
try {
|
|
getSession().lock(instance, ${pojo.importType("org.hibernate.LockMode")}.NONE);
|
|
log.debug("attach successful");
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("attach failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public void delete(${declarationName} persistentInstance) {
|
|
log.debug("deleting ${declarationName} instance");
|
|
try {
|
|
getSession().delete(persistentInstance);
|
|
log.debug("delete successful");
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("delete failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public ${declarationName} merge(${declarationName} detachedInstance) {
|
|
log.debug("merging ${declarationName} instance");
|
|
try {
|
|
${declarationName} result = (${declarationName}) getSession().merge(detachedInstance);
|
|
log.debug("merge successful");
|
|
return result;
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("merge failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
|
|
public List findByCriteria(List criterion) {
|
|
log.debug("finding ${declarationName} instance by criteria");
|
|
try {
|
|
Criteria crit = getSession()<#if c2h.getClassName(clazz).indexOf("Ccrm")!=-1>
|
|
.createCriteria("mnp.database.hb.dto.ccrm.${clazz.entityName}");
|
|
<#else>
|
|
.createCriteria("mnp.database.hb.dto.${clazz.entityName}");
|
|
</#if>
|
|
Iterator iter = criterion.iterator();
|
|
while (iter.hasNext()) {
|
|
crit.add((Criterion) iter.next());
|
|
}
|
|
List results = crit.list();
|
|
log.debug("find by criteria successful, result size: "
|
|
+ results.size());
|
|
return results;
|
|
} catch (RuntimeException e) {
|
|
log.error("find by criteria failed", e);
|
|
throw e;
|
|
} finally {
|
|
closeSession();
|
|
}
|
|
}
|
|
|
|
<#if clazz.identifierProperty?has_content>
|
|
public ${declarationName} findById( ${c2j.getJavaTypeName(clazz.identifierProperty, jdk5)} id) {
|
|
log.debug("getting ${declarationName} instance with id: " + id);
|
|
try {
|
|
<#if c2h.getClassName(clazz).indexOf("Ccrm")!=-1>
|
|
${declarationName} instance = (${declarationName}) getSession()
|
|
.get("mnp.database.hb.dto.ccrm.${clazz.entityName}", id);
|
|
<#else>
|
|
${declarationName} instance = (${declarationName}) getSession()
|
|
.get("mnp.database.hb.dto.${clazz.entityName}", id);
|
|
</#if>
|
|
|
|
if (instance==null) {
|
|
log.debug("get successful, no instance found");
|
|
}
|
|
else {
|
|
log.debug("get successful, instance found");
|
|
}
|
|
return instance;
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("get failed", re);
|
|
throw re;
|
|
}
|
|
finally {
|
|
closeSession();
|
|
}
|
|
}
|
|
</#if>
|
|
|
|
<#if clazz.hasNaturalId()>
|
|
public ${declarationName} findByNaturalId(${c2j.asNaturalIdParameterList(clazz)}) {
|
|
log.debug("getting ${declarationName} instance by natural id");
|
|
try {
|
|
${declarationName} instance = (${declarationName}) getSession()
|
|
.createCriteria("${clazz.entityName}")
|
|
<#if jdk5>
|
|
.add( ${pojo.staticImport("org.hibernate.criterion.Restrictions", "naturalId")}()
|
|
<#else>
|
|
.add( ${pojo.importType("org.hibernate.criterion.Restrictions")}.naturalId()
|
|
</#if>
|
|
<#foreach property in pojo.getAllPropertiesIterator()>
|
|
<#if property.isNaturalIdentifier()>
|
|
.set("${property.name}", ${property.name})
|
|
</#if>
|
|
</#foreach>
|
|
)
|
|
.uniqueResult();
|
|
if (instance==null) {
|
|
log.debug("get successful, no instance found");
|
|
}
|
|
else {
|
|
log.debug("get successful, instance found");
|
|
}
|
|
return instance;
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("query failed", re);
|
|
throw re;
|
|
}
|
|
}
|
|
</#if>
|
|
<#if jdk5>
|
|
public ${pojo.importType("java.util.List")}<${declarationName}> findByExample(${declarationName} instance) {
|
|
<#else>
|
|
public ${pojo.importType("java.util.List")} findByExample(${declarationName} instance) {
|
|
</#if>
|
|
log.debug("finding ${declarationName} instance by example");
|
|
try {
|
|
<#if jdk5>
|
|
${pojo.importType("java.util.List")}<${declarationName}> results = (List<${declarationName}>) sessionFactory.getCurrentSession()
|
|
<#else>
|
|
${pojo.importType("java.util.List")} results = getSession()
|
|
</#if>
|
|
<#if c2h.getClassName(clazz).indexOf("Ccrm")!=-1>
|
|
.createCriteria("mnp.database.hb.dto.ccrm.${clazz.entityName}")
|
|
<#else>
|
|
.createCriteria("mnp.database.hb.dto.${clazz.entityName}")
|
|
</#if>
|
|
|
|
<#if jdk5>
|
|
.add( ${pojo.staticImport("org.hibernate.criterion.Example", "create")}(instance) )
|
|
<#else>
|
|
.add(${pojo.importType("org.hibernate.criterion.Example")}.create(instance))
|
|
</#if>
|
|
.list();
|
|
log.debug("find by example successful, result size: " + results.size());
|
|
return results;
|
|
}
|
|
catch (RuntimeException re) {
|
|
log.error("find by example failed", re);
|
|
throw re;
|
|
}
|
|
finally {
|
|
closeSession();
|
|
}
|
|
}
|
|
<#foreach queryName in cfg.namedQueries.keySet()>
|
|
<#if queryName.startsWith(clazz.entityName + ".")>
|
|
<#assign methname = c2j.unqualify(queryName)>
|
|
<#assign params = cfg.namedQueries.get(queryName).parameterTypes><#assign argList = c2j.asFinderArgumentList(params, pojo)>
|
|
<#if jdk5 && methname.startsWith("find")>
|
|
public ${pojo.importType("java.util.List")}<${declarationName}> ${methname}(${argList}) {
|
|
<#elseif methname.startsWith("count")>
|
|
public int ${methname}(${argList}) {
|
|
<#else>
|
|
public ${pojo.importType("java.util.List")} ${methname}(${argList}) {
|
|
</#if>
|
|
${pojo.importType("org.hibernate.Query")} query = sessionFactory.getCurrentSession()
|
|
.getNamedQuery("${queryName}");
|
|
<#foreach param in params.keySet()>
|
|
<#if param.equals("maxResults")>
|
|
query.setMaxResults(maxResults);
|
|
<#elseif param.equals("firstResult")>
|
|
query.setFirstResult(firstResult);
|
|
<#else>
|
|
query.setParameter("${param}", ${param});
|
|
</#if>
|
|
</#foreach>
|
|
<#if jdk5 && methname.startsWith("find")>
|
|
return (List<${declarationName}>) query.list();
|
|
<#elseif methname.startsWith("count")>
|
|
return ( (Integer) query.uniqueResult() ).intValue();
|
|
<#else>
|
|
return query.list();
|
|
</#if>
|
|
}
|
|
</#if>
|
|
</#foreach></#if>
|
|
}
|
|
</#assign>
|
|
|
|
${pojo.generateImports()}
|
|
${classbody}
|