use of javax.persistence.EntityTransaction in project joynr by bmwcarit.
the class BounceProxyDatabase method updateChannelAssignment.
@Override
public void updateChannelAssignment(String ccid, BounceProxyInformation bpInfo) throws IllegalArgumentException {
logger.trace("updateChannelAssignment(ccid={}, bpId={})", ccid, bpInfo.getId());
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
BounceProxyEntity bpEntity = em.find(BounceProxyEntity.class, bpInfo.getId());
if (bpEntity == null) {
logger.error("No bounce proxy with ID {} registered", bpInfo.getId());
throw new IllegalArgumentException("No bounce proxy with ID '" + bpInfo.getId() + "' registered");
}
ChannelEntity channelEntity = em.find(ChannelEntity.class, ccid);
if (channelEntity == null) {
logger.error("No channel with ID {} was registered before.", ccid);
throw new IllegalArgumentException("No channel with ID '" + ccid + "' was registered before.");
}
// TODO what to do if channel with the same ID is added, but
// different other properties?
bpEntity.addChannel(channelEntity);
em.merge(bpEntity);
tx.commit();
} catch (RuntimeException ex) {
logger.error("Channel assignment could not be persisted. error: {}", ex.getMessage());
if (tx != null && tx.isActive())
tx.rollback();
throw ex;
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class MultithreadedTest method persistFarmWithAnimals.
/**
* Convenience method to create a Farm with 100 Animals. Used by various tests in this file
* @return The id of the Farm object
*/
private Object persistFarmWithAnimals() {
LOG.debug(">> Persisting data");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
Object farmId = "Jones Farm";
try {
tx.begin();
Farm farm = new Farm("Jones Farm");
em.persist(farm);
for (int i = 0; i < 100; i++) {
Animal an = new Animal("Sheep" + i);
an.setFarm(farm);
farm.getAnimals().add(an);
em.persist(an);
}
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception persisting objects", thr);
fail("Exception persisting data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
LOG.debug(">> Persisted data");
// Verify the persistence
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
LOG.debug(">> Querying Animals");
Query q = em.createQuery("SELECT a FROM " + Animal.class.getName() + " a");
List<Animal> ans = q.getResultList();
for (Animal a : ans) {
LOG.debug(">> animal=" + a + " farm=" + a.getFarm());
}
LOG.debug(">> Queried Animals");
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception checking objects", thr);
fail("Exception checking data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
return farmId;
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testOneToOneUniWithOrphanRemovalAndNulling.
/**
* Test of 1-1 Uni relation, and use of orphanRemoval
*/
public void testOneToOneUniWithOrphanRemovalAndNulling() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Bill", "Gates");
Login login = new Login("billy", "$$$$$$");
login.setId(1);
acct.setLogin(login);
em.persist(acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore, and trigger orphanRemoval by nulling
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
assertNotNull(acct.getLogin());
Login login = acct.getLogin();
assertEquals("Login has incorrect username", "billy", login.getUserName());
assertEquals("Login has incorrect password", "$$$$$$", login.getPassword());
// Null the login field so we can trigger orphanRemoval
acct.setLogin(null);
em.flush();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Login login = em.find(Login.class, new Long(1));
assertNull("Login should have been deleted but still exists", login);
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
assertNull(acct.getLogin());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testManyToMany.
/**
* Test of M-N relations.
*/
public void testManyToMany() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
PetroleumSupplier s = new PetroleumSupplier(101, "Esso");
PetroleumCustomer c = new PetroleumCustomer(102, "Brians Fuels");
s.getCustomers().add(c);
c.getSuppliers().add(s);
em.persist(c);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Customers and Suppliers");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// TODO Retrieve the objects and check the persistence
// TODO Do some updates
} finally {
clean(PetroleumSupplier.class);
clean(PetroleumCustomer.class);
}
}
use of javax.persistence.EntityTransaction in project tests by datanucleus.
the class RelationshipsTest method testOneToOneUniWithOrphanRemovalAndDeleting.
/**
* Test of 1-1 Uni relation, and use of orphanRemoval
*/
public void testOneToOneUniWithOrphanRemovalAndDeleting() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = new LoginAccount(1, "Bill", "Gates");
Login login = new Login("billy", "$$$$$$");
login.setId(1);
acct.setLogin(login);
em.persist(acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while creating Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore, and trigger orphanRemoval by nulling
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertEquals("LoginAccount has incorrect firstName", "Bill", acct.getFirstName());
assertEquals("LoginAccount has incorrect lastName", "Gates", acct.getLastName());
assertNotNull(acct.getLogin());
Login login = acct.getLogin();
assertEquals("Login has incorrect username", "billy", login.getUserName());
assertEquals("Login has incorrect password", "$$$$$$", login.getPassword());
// Delete the LoginAccount object so we can trigger orphanRemoval
em.remove(acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount and deleting LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Check the contents of the datastore
em = getEM();
tx = em.getTransaction();
try {
tx.begin();
Login login = em.find(Login.class, new Long(1));
assertNull("Login should have been deleted but still exists", login);
LoginAccount acct = em.find(LoginAccount.class, new Long(1));
assertNull("LoginAccount should have been deleted but still exists", acct);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving Login and LoginAccount");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(LoginAccount.class);
clean(Login.class);
}
}
Aggregations