Example usage for javax.persistence PersistenceException PersistenceException

List of usage examples for javax.persistence PersistenceException PersistenceException

Introduction

In this page you can find the example usage for javax.persistence PersistenceException PersistenceException.

Prototype

public PersistenceException(Throwable cause) 

Source Link

Document

Constructs a new PersistenceException exception with the specified cause.

Usage

From source file:org.castor.jpa.CastorEntityManager.java

/**
 * Make an entity instance managed and persistent.
 * //from w  ww.j a va 2  s .  c  o m
 * @param entity
 * @throws EntityExistsException
 *             if the entity already exists. (The EntityExistsException may
 *             be thrown when the persist operation is invoked, or the
 *             EntityExistsException or another PersistenceException may be
 *             thrown at flush or commit time.)
 * @throws IllegalStateException
 *             if this EntityManager has been closed.
 * @throws IllegalArgumentException
 *             if not an entity
 * @throws TransactionRequiredException
 *             if invoked on a container-managed entity manager of type
 *             PersistenceContextType.TRANSACTION and there is no
 *             transaction.
 */
public void persist(Object entity) {
    // Check whether the entity manager is open.
    verifyOpenEntityManager();
    // Check whether a transaction is running.
    verifyRunningTransaction();

    if (this.context.contains(entity)) {
        // The entity is already managed within this persistence context.
        return;
    }

    try {

        // Create the entity.
        this.database.create(entity);

        // Add the entity to the set of managed entities.
        this.context.manage(entity);

    } catch (ClassNotPersistenceCapableException e) {
        log.error("Entity of type >" + entity.getClass().getName() + "< is not valid entity type.", e);
        throw new IllegalArgumentException(
                "Entity of type >" + entity.getClass().getName() + "< is not valid entity type.", e);
    } catch (DuplicateIdentityException e) {
        log.error("Entity of type " + entity.getClass().getName() + " already exists.");
        throw new EntityExistsException("Entity of type " + entity.getClass().getName() + " already exists.",
                e);
    } catch (org.exolab.castor.jdo.PersistenceException e) {
        throw new PersistenceException(e);
    }
}

From source file:org.guzz.builder.JPA2AnnotationsBuilder.java

public static POJOBasedObjectMapping parseDomainClass(final GuzzContextImpl gf, String dbGroupName,
        String businessName, Class domainCls) throws ClassNotFoundException {
    javax.persistence.Entity pe = (javax.persistence.Entity) domainCls
            .getAnnotation(javax.persistence.Entity.class);
    if (pe == null) {
        throw new PersistenceException(
                "no @javax.persistence.Entity annotation found in class:[" + domainCls + "]");
    }/*from w w w.j  av a 2 s.com*/

    DomainInfo info = new DomainInfo();
    parseClassForEntityTable(info, domainCls);

    //xml definition own high priority.
    String m_dbGroupName = StringUtil.isEmpty(dbGroupName) ? info.dbGroup : dbGroupName;
    String m_businessName = StringUtil.isEmpty(businessName) ? info.businessName : businessName;
    String tableName = info.tableName;
    Class shadow = info.shadow;
    boolean dynamicUpdate = info.dynamicUpdate;

    if (StringUtil.isEmpty(tableName)) {
        //According to the JPA's spefication, we use the class's short name as the table name.
        tableName = domainCls.getSimpleName();
    }

    //The Business name must be defined either in annotation or guzz.xml, or both.
    Assert.assertNotEmpty(m_businessName,
            "business name must be defined. You can declare it either with org.guzz.annotations.Entity annotation in domain class or a-bussiness's attribute in guzz.xml");
    Business business = gf.instanceNewGhost(m_businessName, m_dbGroupName, info.interpreter, domainCls);

    DBGroup dbGroup = gf.getDBGroup(business.getDbGroup());
    Assert.assertNotNull(dbGroup,
            "unknown dbGroup:[" + business.getDbGroup() + "] for domain class:" + domainCls.getName());

    final SimpleTable st = new SimpleTable(dbGroup.getDialect());
    st.setTableName(tableName);
    st.setDynamicUpdate(dynamicUpdate);

    final POJOBasedObjectMapping map = new POJOBasedObjectMapping(gf, dbGroup, st);

    business.setTable(st);
    business.setMapping(map);

    //?business??
    if (business.getName() != null) {
        st.setBusinessName(business.getName());
    } else {
        st.setBusinessName(business.getDomainClass().getName());
    }

    //table?
    JavaBeanWrapper configBeanWrapper = BeanWrapper.createPOJOWrapper(domainCls);
    business.setConfiguredBeanWrapper(configBeanWrapper);

    map.setBusiness(business);

    if (shadow != null && !NullValue.class.isAssignableFrom(shadow)) {
        ShadowTableView sv = (ShadowTableView) BeanCreator.newBeanInstance(shadow);
        sv.setConfiguredTableName(tableName);

        //CustomTableViewShadowTableView
        if (sv instanceof CustomTableView) {
            CustomTableView ctv = (CustomTableView) sv;
            ctv.setConfiguredObjectMapping(map);

            st.setCustomTableView(ctv);
        }

        st.setShadowTableView(sv);
        gf.registerShadowTableView(sv);
    }

    //
    parseClassForAttributes(gf, map, business, dbGroup, st, domainCls);

    //check that the class must own a @Id.
    Assert.assertNotNull(st.getIdentifierGenerator(),
            "no @javax.persistence.Id annotation found in class:[" + domainCls + "]");

    return map;
}

From source file:org.holodeckb2b.ebms3.pulling.PullConfiguration.java

/**
 * Performs additional checks to ensure that the read XML document is valid according to the XSD.
 * <p>Because a general definition is used for reading both the default and specific pullers there is no check
 * on the number of P-Modes referenced by the pullers when the XML is deserialized. Therefor this method performs
 * checks:<ol>//from  w ww. ja  v  a2 s  .  c  om
 * <li>There are no referenced P-Modes for the default puller</li>
 * <li>There is at least one reference P-Mode for each specific puller</li>
 * </ol>
 * 
 * @throws PersistenceException When the read XML fails one of the checks
 */
@Validate
private void validate() throws PersistenceException {
    // Default puller should have no PModes associated with it
    if (defaultPuller.pmodes != null && !defaultPuller.pmodes.isEmpty())
        throw new PersistenceException("The default puller should not specify specific PModes!");

    // A specific puller must specifiy at least one PMode
    if (pullers != null) {
        for (PullerConfig p : pullers)
            if (p.pmodes == null || p.pmodes.isEmpty())
                throw new PersistenceException("Specific puller must reference at least one PMode!");
    }
}

From source file:org.jboss.ejb3.entity.PersistenceXmlLoader.java

public static PersistenceUnitTransactionType getTransactionType(String elementContent) {
    if (StringHelper.isEmpty(elementContent)) {
        return null; //PersistenceUnitTransactionType.JTA;
    } else if (elementContent.equalsIgnoreCase("JTA")) {
        return PersistenceUnitTransactionType.JTA;
    } else if (elementContent.equalsIgnoreCase("RESOURCE_LOCAL")) {
        return PersistenceUnitTransactionType.RESOURCE_LOCAL;
    } else {/* w  ww .java2s.c o  m*/
        throw new PersistenceException("Unknown TransactionType: " + elementContent);
    }
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

@SuppressWarnings("unchecked")
private List<Document> findDocumentsOnFolioOrTranscribeFolio(Integer volNum, String volLetExt, String insertNum,
        String insertLet, Integer folioNum, String folioMod, String rectoVerso, boolean isFolio,
        boolean alsoNullFolio) throws PersistenceException {

    String insNum = org.medici.bia.common.util.StringUtils.safeTrim(insertNum);
    String insLet = org.medici.bia.common.util.StringUtils.safeTrim(insertLet);
    String folMod = org.medici.bia.common.util.StringUtils.safeTrim(folioMod);
    String rv = org.medici.bia.common.util.StringUtils.safeTrim(rectoVerso);

    if (!org.medici.bia.common.util.StringUtils.isNullableString(rv)
            && Document.RectoVerso.convertFromString(rv) == null) {
        throw new PersistenceException("Find document on " + (isFolio ? "" : "transcribe")
                + " folio query: Not possible to convert the given transcribe folio recto/verso [" + rv + "]");
    }/*  www  . j  a v a 2s. c om*/

    StringBuilder sb = new StringBuilder("FROM Document WHERE volume.volNum = :volNum");
    sb.append(" AND volume.volLetExt ").append(
            org.medici.bia.common.util.StringUtils.isNullableString(volLetExt) ? "IS NULL" : "= :volLetExt");
    if (insNum != "") {
        sb.append(" AND insertNum ").append(insNum == null ? "IS NULL" : "= :insertNum");
    }
    if (insLet != "") {
        sb.append(" AND insertLet ").append(insLet == null ? "IS NULL" : "= :insertLet");
    }
    sb.append(" AND ").append(isFolio ? "folioNum " : "transcribeFolioNum ")
            .append(folioNum == null ? "IS NULL" : "= :folioNum");
    if (folMod != "") {
        sb.append(" AND ").append(isFolio ? "folioMod " : "transcribeFolioMod ")
                .append(folMod == null ? "IS NULL" : "= :folioMod");
    }
    if (rv != "" || alsoNullFolio) {
        sb.append(rv != null && alsoNullFolio ? " AND (" : " AND ")
                .append(isFolio ? "folioRectoVerso " : "transcribeFolioRectoVerso ")
                .append(rv == null ? "IS NULL" : "= :rectoVerso");
        if (rv != null && alsoNullFolio) {
            sb.append(" OR ").append(isFolio ? "folioRectoVerso " : "transcribeFolioRectoVerso ")
                    .append(" IS NULL)");
        }
    }

    Query query = getEntityManager().createQuery(sb.toString());
    query.setParameter("volNum", volNum);
    if (!org.medici.bia.common.util.StringUtils.isNullableString(volLetExt)) {
        query.setParameter("volLetExt", volLetExt.trim());
    }
    if (!org.medici.bia.common.util.StringUtils.isNullableString(insNum)) {
        query.setParameter("insertNum", insNum);
    }
    if (!org.medici.bia.common.util.StringUtils.isNullableString(insLet)) {
        query.setParameter("insertLet", insLet);
    }
    if (folioNum != null) {
        query.setParameter("folioNum", folioNum);
    }
    if (!org.medici.bia.common.util.StringUtils.isNullableString(folMod)) {
        query.setParameter("folioMod", folMod);
    }
    if (!org.medici.bia.common.util.StringUtils.isNullableString(rv)) {
        query.setParameter("rectoVerso", Document.RectoVerso.convertFromString(rv));
    }
    return query.getResultList();
}

From source file:org.medici.bia.dao.image.ImageDAOJpaImpl.java

/**
 * {@inheritDoc}//from  w w  w.  j  a  v a 2  s  .  c  om
 */
@Override
public long countImages(Integer volNum, String volLetExt, String imageType, String insertNum, String insertLet,
        Integer folioNum, String folioMod, String rectoVerso) throws PersistenceException {
    Query query = getFindImageQuery(volNum, volLetExt, imageType, insertNum, insertLet, folioNum, folioMod,
            rectoVerso, true);

    Long count;
    try {
        count = (Long) query.getSingleResult();
    } catch (Exception e) {
        throw new PersistenceException(
                "Count Images Query problem: The query [" + query.toString() + "] generates an error!");
    }

    return count;
}

From source file:org.oscarehr.common.dao.DemographicDao.java

public static List<Integer> getDemographicIdsAdmittedIntoFacility(int facilityId) {
    Connection c = null;/*  w  w w .jav a2 s. c  om*/
    try {
        c = DbConnectionFilter.getThreadLocalDbConnection();
        PreparedStatement ps = c.prepareStatement(
                "select distinct(admission.client_id) from admission,program,Facility where admission.program_id=program.id and program.facilityId=?");
        ps.setInt(1, facilityId);
        ResultSet rs = ps.executeQuery();
        ArrayList<Integer> results = new ArrayList<Integer>();
        while (rs.next())
            results.add(rs.getInt(1));
        return (results);
    } catch (SQLException e) {
        throw (new PersistenceException(e));
    } finally {
        SqlUtils.closeResources(c, null, null);
    }
}

From source file:org.oscarehr.common.dao.DemographicDao.java

public static List<Integer> getDemographicIdsAlteredSinceTime(Date value) {
    Connection c = null;//from ww  w.j av a  2 s  .  co m
    try {
        c = DbConnectionFilter.getThreadLocalDbConnection();
        PreparedStatement ps = c.prepareStatement(
                "SELECT DISTINCT demographic_no FROM log WHERE dateTime >= ? and action != 'read'");
        ps.setDate(1, new java.sql.Date(value.getTime()));
        ResultSet rs = ps.executeQuery();
        ArrayList<Integer> results = new ArrayList<Integer>();
        while (rs.next()) {
            if (rs.getInt(1) != 0) {
                results.add(rs.getInt(1));
            }
        }
        return (results);
    } catch (SQLException e) {
        throw (new PersistenceException(e));
    } finally {
        SqlUtils.closeResources(c, null, null);
    }
}

From source file:org.oscarehr.common.dao.DemographicDao.java

public static List<Integer> getDemographicIdsOpenedChartSinceTime(String value) {
    Connection c = null;/*from   w  w  w  . j a v  a2s  .  c  o m*/
    try {
        c = DbConnectionFilter.getThreadLocalDbConnection();
        PreparedStatement ps = c.prepareStatement(
                "SELECT DISTINCT contentId FROM log WHERE dateTime >= ? AND content='eChart' GROUP BY contentId");
        ps.setString(1, value);
        ResultSet rs = ps.executeQuery();
        ArrayList<Integer> results = new ArrayList<Integer>();
        while (rs.next()) {
            results.add(rs.getInt(1));
        }
        return (results);
    } catch (SQLException e) {
        throw (new PersistenceException(e));
    } finally {
        SqlUtils.closeResources(c, null, null);
    }
}

From source file:org.seedstack.jpa.internal.EntityManagerFactoryFactory.java

private EntityManagerFactory createEntityManagerFactory(InternalPersistenceUnitInfo info,
        Properties properties) {/*w  w  w . j a va2s  . c o m*/
    HashMap<String, String> propertiesMap = new HashMap<String, String>();
    if (properties != null) {
        for (Object key : properties.keySet()) {
            propertiesMap.put((String) key, properties.getProperty((String) key));
        }
    }

    EntityManagerFactory fac = null;
    List<PersistenceProvider> persistenceProviders = PersistenceProviderResolverHolder
            .getPersistenceProviderResolver().getPersistenceProviders();

    for (PersistenceProvider persistenceProvider : persistenceProviders) {
        info.setPersistenceProviderClassName(persistenceProvider.getClass().getName());
        fac = persistenceProvider.createContainerEntityManagerFactory(info, propertiesMap);
        if (fac != null) {
            break;
        }
    }

    if (fac == null) {
        throw new PersistenceException(
                "No Persistence provider for EntityManager named " + info.getPersistenceUnitName());
    }

    return fac;
}