List of usage examples for javax.persistence PersistenceException PersistenceException
public PersistenceException(Throwable cause)
PersistenceException
exception with the specified cause. From source file:org.batoo.jpa.core.impl.criteria.jpql.JpqlQuery.java
private void putAlias(BaseQuery<?> q, Tree aliasedDef, final Aliased aliased, final AbstractFrom<?, ?> from) { Map<String, AbstractFrom<?, ?>> aliasMap = this.aliasMap.get(q); if (aliasMap == null) { aliasMap = Maps.newHashMap();//w w w . j av a 2 s.co m this.aliasMap.put(q, aliasMap); } String alias = aliased.getAlias(); if (alias == null) { alias = aliased.getQualified().getSegments().getLast(); from.alias(alias); } if (aliasMap.containsKey(alias)) { throw new PersistenceException("Alias already exists: " + alias + ", line " + aliasedDef.getLine() + ":" + aliasedDef.getCharPositionInLine()); } aliasMap.put(aliased.getAlias(), from); }
From source file:org.batoo.jpa.core.impl.manager.EntityManagerFactoryImpl.java
private Class<?>[] getValidatorsFor(PersistenceParser parser, String group) { final String groups = (String) parser.getProperties().get(group); if (StringUtils.isBlank(groups)) { return new Class[] { Default.class }; }// w w w. ja v a2s. c o m final Set<Class<?>> validationGroups = Sets.newHashSet(); final Iterator<String> i = Splitter.on(",").trimResults().split(groups).iterator(); while (i.hasNext()) { final String className = i.next(); try { validationGroups.add(this.classloader.loadClass(className)); } catch (final ClassNotFoundException e) { throw new PersistenceException("Cannot load class for validation group: " + className); } } return validationGroups.toArray(new Class[validationGroups.size()]); }
From source file:org.batoo.jpa.core.impl.manager.EntityManagerImpl.java
/** * Check if the transaction is valid and belongs to this entity manager. * /*from w ww. j a va 2 s .c om*/ * @param transaction * the transaction to test the validity * * @since 2.0.0 */ public void isValid(EntityTransactionImpl transaction) { if (this.transaction != transaction) { throw new PersistenceException("Transaction is stale"); } }
From source file:org.batoo.jpa.core.impl.manager.EntityManagerImpl.java
/** * Locks the entity./*from w w w.j a v a 2 s .c o m*/ * * @param instance * the managed instance * @param lockMode * the lock mode * @param properties * the properties * * @since 2.0.0 */ public void lock(ManagedInstance<?> instance, LockModeType lockMode, Map<String, Object> properties) { // check optimistic lock is supported if ((lockMode == LockModeType.OPTIMISTIC) || (lockMode == LockModeType.OPTIMISTIC_FORCE_INCREMENT)) { final EntityTypeImpl<?> type = instance.getType().getRootType(); if (!type.hasVersionAttribute()) { throw new PersistenceException( "OPTIMISTIC and OPTIMISTIC_FORCE_INCREMENT not supported on non-versioned entity " + instance.getType().getName()); } try { if (lockMode == LockModeType.OPTIMISTIC_FORCE_INCREMENT) { this.assertTransaction(); instance.incrementVersion(this.getConnection(), true); } else { instance.incrementVersion(this.getConnection(), false); } } catch (final SQLException e) { throw new PersistenceException("Unabled to update the version", e); } } }
From source file:org.batoo.jpa.core.impl.model.attribute.BasicAttribute.java
/** * Fills the sequence / table generated value. * <p>//w w w . ja v a2 s . com * The operation returns false if at least one entity needs to obtain identity from the database. * * @param type * the entity type * @param managedInstance * the managed instance * @param instance * the instance to fill ids. * @return false if all OK, true if if at least one entity needs to obtain identity from the database * * @since 2.0.0 */ @SuppressWarnings("unchecked") public boolean fillValue(EntityTypeImpl<?> type, ManagedInstance<?> managedInstance, Object instance) { T value = this.get(instance); // if the attribute already has value, bail out if (value != null) { return true; } if (this.idType == null) { if (value == null) { value = (T) type.getMappedId(this.getName(), managedInstance.getInstance()); if (value != null) { this.set(instance, value); return true; } } throw new PersistenceException("Ids should be manually assigned"); } Long id; // fill the id switch (this.idType) { case IDENTITY: // indicate a requirement for an implicit flush return false; case SEQUENCE: // fill with the sequence id = this.getMetamodel().getNextSequence(this.generator); this.set(instance, ReflectHelper.convertNumber(id, this.getJavaType())); break; case TABLE: // fill with the next table generator id id = this.getMetamodel().getNextTableValue(this.generator); this.set(instance, ReflectHelper.convertNumber(id, this.getJavaType())); break; case MANUAL: // not possible, manual already handled break; } return true; }
From source file:org.batoo.jpa.core.impl.model.mapping.SingularAssociationMapping.java
/** * {@inheritDoc}/*from ww w. j a v a2s . com*/ * */ @Override public void checkTransient(ManagedInstance<?> managedInstance) { final X instance = this.get(managedInstance.getInstance()); if (instance != null) { final Object instanceId = this.type.getInstanceId(instance); if (instanceId == null) { throw new PersistenceException("Instance " + instance + " is not managed"); } } }
From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java
/** * /*from w ww. java 2s .c o m*/ * @param entityManagerImpl * @param sqlString * @param resultSetMapping * @since $version */ public NativeQuery(EntityManagerImpl entityManager, String sqlString, String resultSetMapping) { super(); this.em = entityManager; this.query = sqlString; this.resultClass = null; this.sqlResultSetMapping = this.em.getMetamodel().getSqlResultSetMapping(resultSetMapping); if (this.sqlResultSetMapping == null) { throw new PersistenceException("SqlResultSetMapping does not exist! : " + resultSetMapping); } else { for (final EntityResultMetadata entityResultMetadata : this.sqlResultSetMapping.getEntities()) { final HashMap<String, Object> _fieldModelMap = Maps.newHashMap(); for (final FieldResultMetadata field : entityResultMetadata.getFields()) { final String[] split; if (field.getName().contains(".")) { split = field.getName().split("\\."); } else { split = new String[] { field.getName(), IdModel.DEFAULT_EMBEDDED_ID, IdModel.DEFAULT_ID }; } final String attr = split[0]; final String embId = split.length > 2 ? split[split.length - 2] : IdModel.DEFAULT_EMBEDDED_ID; final String id = split.length > 1 ? split[split.length - 1] : IdModel.DEFAULT_ID; _fieldModelMap.put(attr, IdModel.merge((IdModel) _fieldModelMap.get(attr), embId, id, field.getColumn())); } this.fieldMap.put(entityResultMetadata.getEntityClass(), _fieldModelMap); } } }
From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java
/** * result set handler for a given resultClass * /*from w w w . jav a2 s. c om*/ * @return result * @param resultSet * @throws SQLException * @since $version */ private List<Object> handleWithResultClass(ResultSet resultSet) throws SQLException { final ArrayList<Object> result = Lists.newArrayList(); final EntityTypeImpl<?> entityType = this.em.getMetamodel().entity(this.resultClass); if (entityType == null) { throw new PersistenceException("Entity Class is not managed :" + this.resultClass); } while (resultSet.next()) {// for each row final ManagedInstance<?> managedInstance = this.handleInstance(resultSet, entityType, null, null); if (managedInstance != null) { result.add(managedInstance.getInstance()); } else { result.add(null); } } return result; }
From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java
/** * result set handler for SqlResultSetMapping annotation data * /*from w ww . j a v a 2 s . c om*/ * @return result * @param resultSet * @throws SQLException * @since $version */ private List<Object> handleWithSqlResultSetMapping(ResultSet resultSet) throws SQLException { final ArrayList<Object> result = Lists.newArrayList(); final ArrayList<Object> resultRow = Lists.newArrayList(); while (resultSet.next()) {// for each row for (final EntityResultMetadata entityResultMetadata : this.sqlResultSetMapping.getEntities()) { final EntityTypeImpl<?> entityType = this.em.getMetamodel() .entity(entityResultMetadata.getEntityClass()); if (entityType == null) { throw new PersistenceException( "Entity Class is not managed :" + entityResultMetadata.getEntityClass()); } final HashMap<String, Object> _fieldMap = this.fieldMap.get(entityType.getJavaType().getName()); final ManagedInstance<?> managedInstance = this.handleInstance(resultSet, entityType, entityResultMetadata.getDiscriminatorColumn(), _fieldMap); if (managedInstance != null) { resultRow.add(managedInstance.getInstance()); } else { resultRow.add(null); } } for (final ColumnResultMetadata columnResultMetadata : this.sqlResultSetMapping.getColumns()) { resultRow.add(resultSet.getObject(columnResultMetadata.getName())); } if (resultRow.size() > 1) { result.add(resultRow.toArray()); } else { result.add(resultRow.get(0)); } resultRow.clear(); } return result; }
From source file:org.batoo.jpa.parser.impl.metadata.type.ManagedTypeMetadatImpl.java
/** * Infers and returns the access type based on all persistence annotations being on fields or methods and parent parent access type. * /* w ww . j a v a 2s . c o m*/ * @param parentAccessType * the parent access type * @return the inferred access type * * @since 2.0.0 */ private AccessType inferAccessType(AccessType parentAccessType) { boolean methodsHasAnnotations = false; boolean fieldsHasAnnotations = false; final List<String> alternated = Lists.newArrayList(); final Field[] fields = this.clazz.getDeclaredFields(); final Method[] methods = this.clazz.getDeclaredMethods(); // find the alternated ones with @Access for (final Method m : methods) { // skip static and private methods. final int mods = m.getModifiers(); if (Modifier.isStatic(mods) || !Modifier.isPublic(mods) || m.isBridge() || m.isSynthetic()) { continue; } if ((m.getParameterTypes().length != 0) || (m.getReturnType() == null)) { continue; } final Access access = m.getAnnotation(Access.class); if (access != null) { final String name = m.getName(); if ((m.getReturnType() == boolean.class) && name.startsWith("is")) { alternated.add(StringUtils.capitalize(name.substring(2))); } else if (name.startsWith("get")) { alternated.add(StringUtils.capitalize(name.substring(3))); } } } for (final Field f : fields) { final Access access = f.getAnnotation(Access.class); if (access != null) { alternated.add(StringUtils.capitalize(f.getName())); } } // check methods for (final Method m : methods) { for (final Annotation a : m.getAnnotations()) { // ignore @Access(PROPERTY) if (a instanceof Access) { if (((Access) a).value() != AccessType.PROPERTY) { continue; } } // ignore @Transient if (a instanceof Transient) { continue; } if ((m.getReturnType() == null) || (m.getParameterTypes().length > 0)) { continue; } String name = a.annotationType().getName(); // ignore the listener annotations if (name.startsWith("javax.persistence.Post") || name.startsWith("javax.persistence.Pre")) { continue; } if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) { name = m.getName(); if ((boolean.class == m.getReturnType()) || name.startsWith("is")) { name = name.substring(2); } else if (name.startsWith("get")) { name = name.substring(3); } if (alternated.contains(StringUtils.capitalize(name))) { continue; } methodsHasAnnotations = true; break; } } } // check fields for (final Field f : fields) { for (final Annotation a : f.getAnnotations()) { // ignore @Access(FIELD) if (a instanceof Access) { if (((Access) a).value() != AccessType.FIELD) { continue; } } // ignore @Transient if (a instanceof Transient) { continue; } final String name = a.annotationType().getName(); if (name.startsWith("javax.persistence") || name.startsWith("org.batoo.jpa.annotation")) { if (alternated.contains(StringUtils.capitalize(f.getName()))) { continue; } fieldsHasAnnotations = true; break; } } } if (fieldsHasAnnotations && methodsHasAnnotations) { throw new PersistenceException( "At least one field and one method has persistence annotations: " + this.clazz.getName()); } if (methodsHasAnnotations) { return AccessType.PROPERTY; } if (fieldsHasAnnotations) { return AccessType.FIELD; } if (parentAccessType != null) { return parentAccessType; } return AccessType.FIELD; }