List of usage examples for javax.persistence PersistenceException PersistenceException
public PersistenceException(String message, Throwable cause)
PersistenceException
exception with the specified detail message and cause. From source file:jef.database.DbUtils.java
/** * RuntimeException/*from w w w . j a v a 2 s . c o m*/ * * @param e * @return */ public static PersistenceException toRuntimeException(SQLException e) { String s = e.getSQLState(); if (e instanceof SQLIntegrityConstraintViolationException) { return new EntityExistsException(e); } else if (e instanceof SQLTimeoutException) { return new QueryTimeoutException(s, e); } return new PersistenceException(s, e); }
From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java
private List<X> buildResultSet(Connection connection, final Object[] parameters) { try {//from w ww .java2 s.c o m this.buildResultSetImpl(connection, parameters); return this.results; } catch (final SQLException e) { QueryImpl.LOG.error(e, "Query failed{0}{1}", QueryImpl.LOG.lazyBoxed(this.getJpql(), this.parameters.entrySet().toArray()), QueryImpl.LOG.lazyBoxed(this.sql, parameters)); this.em.setRollbackOnly(); throw new PersistenceException("Query failed", e); } }
From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java
/** * {@inheritDoc}//from www . j a v a 2 s.c o m * */ @Override public int executeUpdate() { // flush if specified if (!this.q.isInternal() && this.em.hasActiveTransaction() && ((this.flushMode == FlushModeType.AUTO) || (this.em.getFlushMode() == FlushModeType.AUTO))) { this.em.flush(); } final Connection connection = this.em.getConnection(); final Object[] parameters = this.applyParameters(connection); try { this.em.assertTransaction(); return new QueryRunner(this.em.getJdbcAdaptor(), false).update(connection, this.sql, parameters); } catch (final SQLException e) { QueryImpl.LOG.error(e, "Query failed" + QueryImpl.LOG.lazyBoxed(this.sql, parameters)); this.em.setRollbackOnly(); throw new PersistenceException("Query failed", e); } }
From source file:org.batoo.jpa.core.impl.manager.EntityManagerFactoryImpl.java
/** * Returns a lazy created {@link JpqlQuery} for the query. * //w ww. j a va 2 s . co m * @param qlString * the JPQL query string * @return the Jpql Query object * * @since 2.0.0 */ public JpqlQuery getJpqlQuery(String qlString) { try { EntityManagerFactoryImpl.LOG.debug("JPQL: {0}", qlString); JpqlQuery jpqlQuery = this.jpqlCache.get(qlString); if (jpqlQuery == null) { jpqlQuery = new JpqlQuery(EntityManagerFactoryImpl.this, qlString); // clean up job if (this.jpqlCache.size() == EntityManagerFactoryImpl.NO_QUERIES_MAX) { synchronized (this) { if (this.jpqlCache.size() == EntityManagerFactoryImpl.NO_QUERIES_MAX) { final JpqlQuery[] queries = Lists.newArrayList(this.jpqlCache.values()) .toArray(new JpqlQuery[this.jpqlCache.size()]); Arrays.sort(queries, new Comparator<JpqlQuery>() { @Override public int compare(JpqlQuery o1, JpqlQuery o2) { if (o1.getLastUsed() > o2.getLastUsed()) { return 1; } return -1; } }); for (int i = 0; i < EntityManagerFactoryImpl.NO_QUERIES_TRIM; i++) { this.jpqlCache.remove(queries[i].getQueryString()); } } } } this.jpqlCache.put(qlString, jpqlQuery); } return jpqlQuery; } catch (final Exception e) { if (e.getCause() instanceof PersistenceException) { throw (PersistenceException) e.getCause(); } if (e.getCause() instanceof IllegalArgumentException) { throw (IllegalArgumentException) e.getCause(); } throw new PersistenceException("Cannot parse query: " + qlString, e); } }
From source file:org.batoo.jpa.core.impl.manager.EntityManagerImpl.java
/** * {@inheritDoc}/*from w w w . j a va 2 s . c o m*/ * */ @Override public void flush() { if (this.inFlush) { return; } this.assertTransaction(); this.inFlush = true; try { this.session.handleExternals(); final ManagedInstance<?>[] instances = this.session.handleAdditions(); this.session.cascadeRemovals(instances); this.session.handleOrphans(instances); this.session.flush(this.getConnection()); } catch (final SQLException e) { EntityManagerImpl.LOG.error(e, "Flush failed"); throw new PersistenceException("Flush failed", e); } catch (final ConstraintViolationException e) { EntityManagerImpl.LOG.debug(e, "Flush failed due to validation errors:\n\t" + Joiner.on("\n\t").join(e.getConstraintViolations())); throw e; } catch (final RuntimeException e) { EntityManagerImpl.LOG.error(e, "Flush failed"); throw e; } finally { this.inFlush = false; } }
From source file:org.batoo.jpa.core.impl.manager.EntityManagerImpl.java
/** * Returns the active connection./*from w w w .ja v a2 s. c o m*/ * * @return the connection * * @since 2.0.0 */ public Connection getConnection() { // if the connection exists then simply return it if (this.connection != null) { return this.connection; } try { this.joinTransaction(); // create a new connection and return it return this.connection = this.datasource.getConnection(); } catch (final SQLException e) { throw new PersistenceException("Unable to obtain connection from the datasource", e); } }
From source file:org.batoo.jpa.core.impl.model.EntityTypeImpl.java
/** * Creates a new managed instance with the id. * /*www. j a va 2 s . co m*/ * @param session * the session * @param id * the primary key * @param lazy * if the instance is lazy * @return the managed instance created * * @since 2.0.0 */ @SuppressWarnings({ "unchecked" }) public ManagedInstance<X> getManagedInstanceById(SessionImpl session, ManagedId<X> id, boolean lazy) { try { final X instance = (X) this.constructor .newInstance(new Object[] { this.getJavaType(), session, id.getId(), !lazy }); final ManagedInstance<X> managedInstance = new ManagedInstance<X>(this, session, instance, id); ((EnhancedInstance) instance).__enhanced__$$__setManagedInstance(managedInstance); return managedInstance; } catch (final Exception e) { throw new PersistenceException("Cannot create instance " + id, e); } // not possible }
From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java
/** * {@inheritDoc}//from w w w. ja va2s .com * */ @Override public int executeUpdate() { this.em.assertTransaction(); // flush if specified if ((this.flushMode == FlushModeType.AUTO) || (this.em.getFlushMode() == FlushModeType.AUTO)) { this.em.flush(); } try { if (!this.parameters.isEmpty()) { final Object[] parameters = new Object[this.parameters.size()]; for (int i = 0; i < this.parameters.size(); i++) { parameters[i] = this.getParameterValue(i); } return new QueryRunner(this.em.getJdbcAdaptor(), false).update(this.query, parameters); } return new QueryRunner(this.em.getJdbcAdaptor(), false).update(this.query); } catch (final SQLException e) { throw new PersistenceException("Native query execution has failed!", e); } }
From source file:org.batoo.jpa.core.impl.nativeQuery.NativeQuery.java
private List<?> getResultListImpl() { this.em.getSession().setLoadTracker(); try {/*from w ww . j av a 2 s. c o m*/ final Object[] paramValues = new Object[this.parameters.size()]; for (int i = 0; i < paramValues.length; i++) { paramValues[i] = this.getParameterValue(i + 1); } try { return this.results = new QueryRunner(this.em.getJdbcAdaptor(), false) .query(this.em.getConnection(), this.query, this, paramValues); } catch (final SQLException e) { throw new PersistenceException("Native query execution failed!", e); } } finally { this.em.getSession().releaseLoadTracker(); } }
From source file:org.batoo.jpa.jdbc.AbstractColumn.java
/** * {@inheritDoc}/*from ww w.j av a 2 s . c o m*/ * */ @Override public Object convertValue(Connection connection, final Object value) { if (value == null) { return null; } if (this.temporalType != null) { switch (this.temporalType) { case DATE: if (value instanceof java.sql.Date) { return value; } if (value instanceof Date) { return new java.sql.Date(((Date) value).getTime()); } return new java.sql.Date(((Calendar) value).getTimeInMillis()); case TIME: if (value instanceof java.sql.Time) { return value; } if (value instanceof Date) { return new java.sql.Time(((Date) value).getTime()); } return new java.sql.Time(((Calendar) value).getTimeInMillis()); case TIMESTAMP: if (value instanceof java.sql.Timestamp) { return value; } if (value instanceof Date) { return new java.sql.Timestamp(((Date) value).getTime()); } return new java.sql.Timestamp(((Calendar) value).getTimeInMillis()); } } if (this.numberType != null) { return ReflectHelper.convertNumber((Number) value, this.numberType); } if (this.enumType != null) { final Enum<?> enumValue = (Enum<?>) value; if (this.enumType == EnumType.ORDINAL) { return enumValue.ordinal(); } return enumValue.name(); } if (this.lob) { try { if (this.javaType == String.class) { return new SerialClob(((String) value).toCharArray()); } else if (this.javaType == char[].class) { return new SerialClob((char[]) value); } else if (this.javaType == byte[].class) { return new SerialBlob((byte[]) value); } else { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final ObjectOutputStream oos = new ObjectOutputStream(os); try { oos.writeObject(value); } finally { oos.close(); } return new SerialBlob(os.toByteArray()); } } catch (final Exception e) { throw new PersistenceException("Cannot set parameter", e); } } else { return value; } }