1 package org.hibernate.ejb; 3 4 import java.util.Calendar ; 5 import java.util.Collection ; 6 import java.util.Date ; 7 import java.util.List ; 8 import javax.persistence.EntityNotFoundException; 9 import javax.persistence.FlushModeType; 10 import javax.persistence.Query; 11 import javax.persistence.TemporalType; 12 13 import org.hibernate.CacheMode; 14 import org.hibernate.FlushMode; 15 import org.hibernate.NonUniqueResultException; 16 17 import static javax.persistence.TemporalType.DATE; 18 import static javax.persistence.TemporalType.TIME; 19 import static javax.persistence.TemporalType.TIMESTAMP; 20 21 25 public class QueryImpl implements Query { 26 private org.hibernate.Query query; 27 28 public QueryImpl(org.hibernate.Query query) { 29 this.query = query; 30 } 31 32 public org.hibernate.Query getHibernateQuery() { 33 return query; 34 } 35 36 public int executeUpdate() { 37 return query.executeUpdate(); 38 } 39 40 public List getResultList() { 41 return query.list(); 42 } 43 44 public Object getSingleResult() { 45 try { 46 Object result = query.uniqueResult(); 47 48 if ( result == null ) { 49 throw new EntityNotFoundException( "No entity found for query" ); 50 } 51 52 return result; 53 } 54 catch (NonUniqueResultException e) { 55 throw new javax.persistence.NonUniqueResultException( e.getMessage() ); 56 } 57 } 58 59 public Query setMaxResults(int maxResult) { 60 if ( maxResult < 0 ) { 61 throw new IllegalArgumentException ( 62 "Negative (" 63 + maxResult 64 + ") parameter passed in to setMaxResults" 65 ); 66 } 67 query.setMaxResults( maxResult ); 68 return this; 69 } 70 71 public Query setFirstResult(int firstResult) { 72 if ( firstResult < 0 ) { 73 throw new IllegalArgumentException ( 74 "Negative (" 75 + firstResult 76 + ") parameter passed in to setMaxResults" 77 ); 78 } 79 query.setFirstResult( firstResult ); 80 return this; 81 } 82 83 public Query setHint(String hintName, Object value) { 84 try { 85 if ( "org.hibernate.timeout".equals( hintName ) ) { 86 query.setTimeout( (Integer ) value ); 87 } 88 else if ( "org.hibernate.comment".equals( hintName ) ) { 89 query.setComment( (String ) value ); 90 } 91 else if ( "org.hibernate.fetchSize".equals( hintName ) ) { 92 query.setFetchSize( (Integer ) value ); 93 } 94 else if ( "org.hibernate.cacheRegion".equals( hintName ) ) { 95 query.setCacheRegion( (String ) value ); 96 } 97 else if ( "org.hibernate.cacheable".equals( hintName ) ) { 98 query.setCacheable( (Boolean ) value ); 99 } 100 else if ( "org.hibernate.readOnly".equals( hintName ) ) { 101 query.setReadOnly( (Boolean ) value ); 102 } 103 else if ( "org.hibernate.cacheMode".equals( hintName ) ) { 104 query.setCacheMode( (CacheMode) value ); 105 } 106 } 107 catch (ClassCastException e) { 108 throw new IllegalArgumentException ( "Value for hint" ); 109 } 110 return this; 111 } 112 113 public Query setParameter(String name, Object value) { 114 if ( value instanceof Collection ) { 115 query.setParameterList( name, (Collection ) value ); 116 } 117 else { 118 query.setParameter( name, value ); 119 } 120 return this; 121 } 122 123 public Query setParameter(String name, Date value, TemporalType temporalType) { 124 if ( temporalType == DATE ) { 125 query.setDate( name, value ); 126 } 127 else if ( temporalType == TIME ) { 128 query.setTime( name, value ); 129 } 130 else if ( temporalType == TIMESTAMP ) { 131 query.setTimestamp( name, value ); 132 } 133 return this; 134 } 135 136 public Query setParameter(String name, Calendar value, TemporalType temporalType) { 137 if ( temporalType == DATE ) { 138 query.setCalendarDate( name, value ); 139 } 140 else if ( temporalType == TIME ) { 141 throw new IllegalArgumentException ( "not yet implemented" ); 142 } 143 else if ( temporalType == TIMESTAMP ) { 144 query.setCalendar( name, value ); 145 } 146 return this; 147 } 148 149 public Query setParameter(int position, Object value) { 150 query.setParameter( position, value ); 151 return this; 152 } 153 154 public Query setParameter(int position, Date value, TemporalType temporalType) { 155 if ( temporalType == DATE ) { 156 query.setDate( position, value ); 157 } 158 else if ( temporalType == TIME ) { 159 query.setTime( position, value ); 160 } 161 else if ( temporalType == TIMESTAMP ) { 162 query.setTimestamp( position, value ); 163 } 164 return this; 165 } 166 167 public Query setParameter(int position, Calendar value, TemporalType temporalType) { 168 if ( temporalType == DATE ) { 169 query.setCalendarDate( position, value ); 170 } 171 else if ( temporalType == TIME ) { 172 throw new IllegalArgumentException ( "not yet implemented" ); 173 } 174 else if ( temporalType == TIMESTAMP ) { 175 query.setCalendar( position, value ); 176 } 177 return this; 178 } 179 180 public Query setFlushMode(FlushModeType flushMode) { 181 if ( flushMode == FlushModeType.AUTO ) { 182 query.setFlushMode( FlushMode.AUTO ); 183 } 184 else if ( flushMode == FlushModeType.COMMIT ) { 185 query.setFlushMode( FlushMode.COMMIT ); 186 } 187 else if ( flushMode == FlushModeType.NEVER ) { 188 query.setFlushMode( FlushMode.NEVER ); 189 } 190 return this; 191 } 192 } 193 | Popular Tags |