Search in sources :

Example 56 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class MergeTest method testMergeStaleVersionFails.

@Test
public void testMergeStaleVersionFails() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    VersionedEntity entity = new VersionedEntity("entity", "entity");
    s.persist(entity);
    s.getTransaction().commit();
    s.close();
    // make the detached 'entity' reference stale...
    s = openSession();
    s.beginTransaction();
    VersionedEntity entity2 = (VersionedEntity) s.get(VersionedEntity.class, entity.getId());
    entity2.setName("entity-name");
    s.getTransaction().commit();
    s.close();
    // now try to reattch it
    s = openSession();
    s.beginTransaction();
    try {
        s.merge(entity);
        s.getTransaction().commit();
        fail("was expecting staleness error");
    } catch (PersistenceException e) {
        // expected
        assertTyping(StaleObjectStateException.class, e.getCause());
    } finally {
        s.getTransaction().rollback();
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 57 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class OptimisticLockTest method testDeleteOptimisticLockFailure.

private void testDeleteOptimisticLockFailure(String entityName) {
    Session mainSession = openSession();
    mainSession.beginTransaction();
    Document doc = new Document();
    doc.setTitle("Hibernate in Action");
    doc.setAuthor("Bauer et al");
    doc.setSummary("Very boring book about persistence");
    doc.setText("blah blah yada yada yada");
    doc.setPubDate(new PublicationDate(2004));
    mainSession.save(entityName, doc);
    mainSession.flush();
    doc.setSummary("A modern classic");
    mainSession.flush();
    doc.getPubDate().setMonth(Integer.valueOf(3));
    mainSession.flush();
    mainSession.getTransaction().commit();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.get(entityName, doc.getId());
    Session otherSession = openSession();
    otherSession.beginTransaction();
    Document otherDoc = (Document) otherSession.get(entityName, doc.getId());
    otherDoc.setSummary("my other summary");
    otherSession.flush();
    otherSession.getTransaction().commit();
    otherSession.close();
    try {
        mainSession.delete(doc);
        mainSession.flush();
        fail("expecting opt lock failure");
    } catch (StaleObjectStateException e) {
    // expected
    } catch (PersistenceException e) {
        // expected
        checkException(mainSession, e);
    }
    mainSession.clear();
    mainSession.getTransaction().rollback();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.load(entityName, doc.getId());
    mainSession.delete(entityName, doc);
    mainSession.getTransaction().commit();
    mainSession.close();
}
Also used : PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session)

Example 58 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class OptimisticLockTest method testUpdateOptimisticLockFailure.

private void testUpdateOptimisticLockFailure(String entityName) {
    Session mainSession = openSession();
    mainSession.beginTransaction();
    Document doc = new Document();
    doc.setTitle("Hibernate in Action");
    doc.setAuthor("Bauer et al");
    doc.setSummary("Very boring book about persistence");
    doc.setText("blah blah yada yada yada");
    doc.setPubDate(new PublicationDate(2004));
    mainSession.save(entityName, doc);
    mainSession.getTransaction().commit();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.get(entityName, doc.getId());
    Session otherSession = sessionFactory().openSession();
    otherSession.beginTransaction();
    Document otherDoc = (Document) otherSession.get(entityName, doc.getId());
    otherDoc.setSummary("A modern classic");
    otherSession.getTransaction().commit();
    otherSession.close();
    try {
        doc.setSummary("A machiavellian achievement of epic proportions");
        mainSession.flush();
        fail("expecting opt lock failure");
    } catch (PersistenceException e) {
        // expected
        checkException(mainSession, e);
    }
    mainSession.clear();
    mainSession.getTransaction().rollback();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.load(entityName, doc.getId());
    mainSession.delete(entityName, doc);
    mainSession.getTransaction().commit();
    mainSession.close();
}
Also used : PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session)

Example 59 with PersistenceException

use of javax.persistence.PersistenceException in project ORCID-Source by ORCID.

the class ResearcherUrlDaoTest method testCannotAddDuplicatedResearcherUrl.

@Test
@Rollback(true)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testCannotAddDuplicatedResearcherUrl() {
    try {
        ResearcherUrlEntity newRUrl = new ResearcherUrlEntity();
        newRUrl.setDateCreated(new Date());
        newRUrl.setLastModified(new Date());
        newRUrl.setClientSourceId("4444-4444-4444-4443");
        newRUrl.setUrl("http://www.researcherurl2.com?id=1");
        newRUrl.setUrlName("test");
        newRUrl.setUser(new ProfileEntity("4444-4444-4444-4443"));
        newRUrl.setVisibility(Visibility.PUBLIC);
        newRUrl = researcherUrlDao.merge(newRUrl);
        assertNotNull(newRUrl);
        fail();
    } catch (PersistenceException e) {
    }
}
Also used : ResearcherUrlEntity(org.orcid.persistence.jpa.entities.ResearcherUrlEntity) PersistenceException(javax.persistence.PersistenceException) Date(java.util.Date) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity) Test(org.junit.Test) DBUnitTest(org.orcid.test.DBUnitTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 60 with PersistenceException

use of javax.persistence.PersistenceException in project Asqatasun by Asqatasun.

the class CSSJsoupPhlocContentAdapterImpl method adaptExternalCss.

/**
     * Adapt the external css. 
     */
private void adaptExternalCss() {
    for (Element el : externalCssElements) {
        List<CSSMediaQuery> mediaList = getListOfMediaFromAttributeValue(el);
        String resourcePath = el.attr("abs:href");
        getExternalResourceAndAdapt(resourcePath, mediaList);
    }
    Set<Long> relatedCssIdSet = new HashSet<>();
    // At the end of the document we link each external css that are
    // already fetched and that have been encountered in the SSP to the SSP.
    LOGGER.debug("Found " + relatedExternalCssSet.size() + " external css in " + getSSP().getURI());
    for (StylesheetContent cssContent : relatedExternalCssSet) {
        if (cssContent.getAdaptedContent() == null) {
            cssContent.setAdaptedContent(CSS_ON_ERROR);
        }
        LOGGER.debug("Create relation between " + getSSP().getURI() + " and " + cssContent.getURI());
        // to avoid fatal error when persist weird sourceCode
        try {
            // with the current SSP
            if (cssContent.getId() == null) {
                cssContent = (StylesheetContent) getContentDataService().saveOrUpdate(cssContent);
            }
            relatedCssIdSet.add(cssContent.getId());
        } catch (PersistenceException | DataException pe) {
            adaptedContentOnError(cssContent, relatedCssIdSet);
        }
    }
    getContentDataService().saveContentRelationShip(getSSP(), relatedCssIdSet);
}
Also used : CSSMediaQuery(com.phloc.css.decl.CSSMediaQuery) StylesheetContent(org.asqatasun.entity.audit.StylesheetContent) DataException(org.hibernate.exception.DataException) Element(org.jsoup.nodes.Element) PersistenceException(javax.persistence.PersistenceException)

Aggregations

PersistenceException (javax.persistence.PersistenceException)125 Test (org.junit.Test)66 Session (org.hibernate.Session)50 Transaction (org.hibernate.Transaction)29 EntityManager (javax.persistence.EntityManager)17 IOException (java.io.IOException)12 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)9 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 TransactionRequiredException (javax.persistence.TransactionRequiredException)7 MessagesEvent (com.openmeap.event.MessagesEvent)5 EntityNotFoundException (javax.persistence.EntityNotFoundException)5 OptimisticLockException (javax.persistence.OptimisticLockException)5 TestForIssue (org.hibernate.testing.TestForIssue)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)4 NoResultException (javax.persistence.NoResultException)4 LockOptions (org.hibernate.LockOptions)4 StaleStateException (org.hibernate.StaleStateException)4