use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class CustomSchema method storeLoadCheckSingleRowInvalid.
@Test(expectedExceptions = org.hibernate.exception.ConstraintViolationException.class)
public void storeLoadCheckSingleRowInvalid() throws Throwable {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
// Wrong start/end time
Item item = new Item("Some Item", CalendarUtil.TOMORROW.getTime(), CalendarUtil.TODAY.getTime());
em.persist(item);
try {
em.flush();
} catch (Exception ex) {
throw unwrapCauseOfType(ex, org.hibernate.exception.ConstraintViolationException.class);
}
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class CustomSchema method storeLoadValid.
// The control
@Test
public void storeLoadValid() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
User user = new User();
user.setEmail("valid@test.com");
user.setUsername("someuser");
em.persist(user);
user = new User();
user.setEmail("valid2@test.com");
user.setUsername("otheruser");
em.persist(user);
tx.commit();
em.close();
tx.begin();
em = JPA.createEntityManager();
user = em.find(User.class, user.getId());
assertEquals(user.getUsername(), "otheruser");
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class HelloWorldJPA method storeLoadMessage.
@Test
public void storeLoadMessage() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
try {
{
/*
Get access to the standard transaction API <code>UserTransaction</code> and
begin a transaction on this thread of execution.
*/
UserTransaction tx = TM.getUserTransaction();
tx.begin();
/*
Begin a new session with the database by creating an <code>EntityManager</code>, this
is your context for all persistence operations.
*/
EntityManager em = emf.createEntityManager();
/*
Create a new instance of the mapped domain model class <code>Message</code> and
set its <code>text</code> property.
*/
Message message = new Message();
message.setText("Hello World!");
/*
Enlist the transient instance with your persistence context, you make it persistent.
Hibernate now knows that you wish to store that data, it doesn't necessarily call the
database immediately, however.
*/
em.persist(message);
/*
Commit the transaction, Hibernate now automatically checks the persistence context and
executes the necessary SQL <code>INSERT</code> statement.
*/
tx.commit();
// INSERT into MESSAGE (ID, TEXT) values (1, 'Hello World!')
/*
If you create an <code>EntityManager</code>, you must close it.
*/
em.close();
}
{
/*
Every interaction with your database should occur within explicit transaction boundaries,
even if you are only reading data.
*/
UserTransaction tx = TM.getUserTransaction();
tx.begin();
EntityManager em = emf.createEntityManager();
/*
Execute a query to retrieve all instances of <code>Message</code> from the database.
*/
List<Message> messages = em.createQuery("select m from Message m").getResultList();
// SELECT * from MESSAGE
assertEquals(messages.size(), 1);
assertEquals(messages.get(0).getText(), "Hello World!");
/*
You can change the value of a property, Hibernate will detect this automatically because
the loaded <code>Message</code> is still attached to the persistence context it was loaded in.
*/
messages.get(0).setText("Take me to your leader!");
/*
On commit, Hibernate checks the persistence context for dirty state and executes the
SQL <code>UPDATE</code> automatically to synchronize the in-memory with the database state.
*/
tx.commit();
// UPDATE MESSAGE set TEXT = 'Take me to your leader!' where ID = 1
em.close();
}
} finally {
TM.rollback();
emf.close();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class AccessType method storeLoadAccessType.
@Test
public void storeLoadAccessType() throws Exception {
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
Item someItem = new Item();
someItem.setName("Some item");
someItem.setDescription("This is some description.");
em.persist(someItem);
tx.commit();
em.close();
Long ITEM_ID = someItem.getId();
tx.begin();
em = JPA.createEntityManager();
Item item = em.find(Item.class, ITEM_ID);
assertEquals(item.getName(), "AUCTION: Some item");
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.transaction.UserTransaction in project microservices by pwillhan.
the class DerivedProperties method storeItemAndBids.
public Long storeItemAndBids() throws Exception {
UserTransaction tx = TM.getUserTransaction();
tx.begin();
EntityManager em = JPA.createEntityManager();
Item item = new Item();
item.setName("Some item");
item.setDescription("This is some description.");
em.persist(item);
for (int i = 1; i <= 3; i++) {
Bid bid = new Bid();
bid.setAmount(new BigDecimal(10 + i));
bid.setItem(item);
em.persist(bid);
}
tx.commit();
em.close();
return item.getId();
}
Aggregations