1 package org.hibernate.criterion; 3 4 import java.io.Serializable ; 5 6 import org.hibernate.Criteria; 7 import org.hibernate.FetchMode; 8 import org.hibernate.HibernateException; 9 import org.hibernate.Session; 10 import org.hibernate.impl.CriteriaImpl; 11 import org.hibernate.impl.SessionImpl; 12 import org.hibernate.transform.ResultTransformer; 13 14 26 public class DetachedCriteria implements CriteriaSpecification, Serializable { 27 28 private final CriteriaImpl impl; 29 private final Criteria criteria; 30 31 protected DetachedCriteria(String entityName) { 32 impl = new CriteriaImpl(entityName, null); 33 criteria = impl; 34 } 35 36 protected DetachedCriteria(String entityName, String alias) { 37 impl = new CriteriaImpl(entityName, alias, null); 38 criteria = impl; 39 } 40 41 protected DetachedCriteria(CriteriaImpl impl, Criteria criteria) { 42 this.impl = impl; 43 this.criteria = criteria; 44 } 45 46 50 public Criteria getExecutableCriteria(Session session) { 51 impl.setSession( (SessionImpl) session ); 52 return impl; 53 } 54 55 public static DetachedCriteria forEntityName(String entityName) { 56 return new DetachedCriteria(entityName); 57 } 58 59 public static DetachedCriteria forEntityName(String entityName, String alias) { 60 return new DetachedCriteria(entityName, alias); 61 } 62 63 public static DetachedCriteria forClass(Class clazz) { 64 return new DetachedCriteria( clazz.getName() ); 65 } 66 67 public static DetachedCriteria forClass(Class clazz, String alias) { 68 return new DetachedCriteria( clazz.getName() , alias ); 69 } 70 71 public DetachedCriteria add(Criterion criterion) { 72 criteria.add(criterion); 73 return this; 74 } 75 76 public DetachedCriteria addOrder(Order order) { 77 criteria.addOrder(order); 78 return this; 79 } 80 81 public DetachedCriteria createAlias(String associationPath, String alias) 82 throws HibernateException { 83 criteria.createAlias(associationPath, alias); 84 return this; 85 } 86 87 public DetachedCriteria createCriteria(String associationPath, String alias) 88 throws HibernateException { 89 return new DetachedCriteria( impl, criteria.createCriteria(associationPath) ); 90 } 91 92 public DetachedCriteria createCriteria(String associationPath) 93 throws HibernateException { 94 return new DetachedCriteria( impl, criteria.createCriteria(associationPath) ); 95 } 96 97 public String getAlias() { 98 return criteria.getAlias(); 99 } 100 101 public DetachedCriteria setFetchMode(String associationPath, FetchMode mode) 102 throws HibernateException { 103 criteria.setFetchMode(associationPath, mode); 104 return this; 105 } 106 107 public DetachedCriteria setProjection(Projection projection) { 108 criteria.setProjection(projection); 109 return this; 110 } 111 112 public DetachedCriteria setResultTransformer(ResultTransformer resultTransformer) { 113 criteria.setResultTransformer(resultTransformer); 114 return this; 115 } 116 117 public String toString() { 118 return "DetachableCriteria(" + criteria.toString() + ')'; 119 } 120 121 CriteriaImpl getCriteriaImpl() { 122 return impl; 123 } 124 } 125 | Popular Tags |