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();
}
}
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();
}
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();
}
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) {
}
}
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);
}
Aggregations