Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Description: Cannot use an EntityTransaction while using JTA.  RSS feed

 
Raghu Sha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The below are the environment
Glassfish 3.1.1
Derby
Eclipse link
JTA

Hi,

When trying to delete parent table, child records also should delete. Used cascasde on delete.

Customer.java
@OneToMany(cascade=ALL, mappedBy="customer")
public Collection<Order> getOrders() {
return orders;
}

Order.java
@ManyToOne()
@JoinColumn(name="CUSTOMER_ID")
public Customer getCustomer() {
return customer;
}


entityManager.getTransaction().begin();
Customer customer = entityManager.find(Custom.class, audit.getCustomerId());
entityManager.remove(order);
entityManager.getTransaction().commit();


When try to remove parent entity, getting exception as
Exception Description: Cannot use an EntityTransaction while using JTA.

persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EJBPU">
<jta-data-source>jdbc/ejb_pu_sample</jta-data-source>

How to solve?








 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to handle transactions manually if you are working with an application server? After all container-managed JTA transactions are one of the big advantages of using application servers. You should simply let the container take care of transactions and your problem is gone. Within a managed JEE environment there's the concept of user transactions but I guess for 99% of all use cases container-managed JTA transactions are fine.

To use JTA transactions you simply have to let the container inject an instance of EntityManager into your EJBs and use that for your persistence logic. Then you don't have to worry about handling transactions. The default behavior is OK for lots of use cases and can be further customized declaratively with annotations on your EJB methods.

Additionally you should have a look at the JEE tutorial for an explanation on this topic.

Marco
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!