1 19 package org.apache.cayenne.jpa; 20 21 import javax.persistence.EntityTransaction; 22 import javax.persistence.FlushModeType; 23 import javax.persistence.LockModeType; 24 import javax.persistence.Query; 25 26 import org.apache.cayenne.DataChannel; 27 import org.apache.cayenne.Persistent; 28 29 35 public class TypeCheckingEntityManager implements CayenneEntityManager { 39 40 protected CayenneEntityManager entityManager; 41 42 public TypeCheckingEntityManager(CayenneEntityManager entityManager) { 43 this.entityManager = entityManager; 44 } 45 46 protected void checkEntityType(Class entityClass) throws IllegalArgumentException { 47 if (entityClass == null) { 48 throw new IllegalArgumentException ("Null entity class"); 49 } 50 51 if (!Persistent.class.isAssignableFrom(entityClass)) { 52 throw new IllegalArgumentException ("Entity class must be Persistent, got: " 53 + entityClass.getName()); 54 } 55 } 56 57 protected void checkEntityType(Object entity) throws IllegalArgumentException { 58 if (entity == null) { 59 throw new IllegalArgumentException ("Null entity"); 60 } 61 62 if (!(entity instanceof Persistent)) { 63 String className = (entity != null) ? entity.getClass().getName() : "<null>"; 64 throw new IllegalArgumentException ("entity must be Persistent: " + className); 65 } 66 } 67 68 protected void enhance(Class entityClass) { 69 70 } 71 72 public void clear() { 73 entityManager.clear(); 74 } 75 76 public void close() { 77 entityManager.close(); 78 } 79 80 public boolean contains(Object entity) { 81 checkEntityType(entity); 82 return entityManager.contains(entity); 83 } 84 85 public Query createNamedQuery(String name) { 86 return entityManager.createNamedQuery(name); 87 } 88 89 public Query createNativeQuery(String sqlString, Class resultClass) { 90 checkEntityType(resultClass); 91 return entityManager.createNativeQuery(sqlString, resultClass); 92 } 93 94 public Query createNativeQuery(String sqlString, String resultSetMapping) { 95 return entityManager.createNativeQuery(sqlString, resultSetMapping); 96 } 97 98 public Query createNativeQuery(String sqlString) { 99 return entityManager.createNativeQuery(sqlString); 100 } 101 102 public Query createQuery(String ejbqlString) { 103 return entityManager.createQuery(ejbqlString); 104 } 105 106 public <T> T find(Class <T> entityClass, Object primaryKey) { 107 checkEntityType(entityClass); 108 return entityManager.find(entityClass, primaryKey); 109 } 110 111 public void flush() { 112 entityManager.flush(); 113 } 114 115 public DataChannel getChannel() { 116 return entityManager.getChannel(); 117 } 118 119 public Object getDelegate() { 120 return entityManager.getDelegate(); 121 } 122 123 public FlushModeType getFlushMode() { 124 return entityManager.getFlushMode(); 125 } 126 127 public <T> T getReference(Class <T> entityClass, Object primaryKey) { 128 return entityManager.getReference(entityClass, primaryKey); 129 } 130 131 public EntityTransaction getTransaction() { 132 return entityManager.getTransaction(); 133 } 134 135 public boolean isOpen() { 136 return entityManager.isOpen(); 137 } 138 139 public void joinTransaction() { 140 entityManager.joinTransaction(); 141 } 142 143 public void lock(Object entity, LockModeType lockMode) { 144 entityManager.lock(entity, lockMode); 145 } 146 147 public <T> T merge(T entity) { 148 checkEntityType(entity); 149 return entityManager.merge(entity); 150 } 151 152 public void persist(Object entity) { 153 checkEntityType(entity); 154 entityManager.persist(entity); 155 } 156 157 public void refresh(Object entity) { 158 entityManager.refresh(entity); 159 } 160 161 public void remove(Object entity) { 162 checkEntityType(entity); 163 entityManager.remove(entity); 164 } 165 166 public void setFlushMode(FlushModeType flushMode) { 167 entityManager.setFlushMode(flushMode); 168 } 169 } 170 | Popular Tags |