-1

am using JPA 2 and EJB 3.1

Have the following Entity:

@Entity
public class SVPDiscovery implements Serializable {
private static final long serialVersionUID = 1L;
public SVPDiscovery() {
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

private String objectID;

//bi-directional many-to-one association to Party
@ManyToOne
@JoinColumn(name="EPCISREPOID")
private EpcisRepository EPCISREPOID;

@ManyToMany (cascade=CascadeType.ALL)
@JoinTable(name = "SVPDiscovery_ACL"
, joinColumns = { @JoinColumn(name = "SVPDiscoveryID") }
, inverseJoinColumns = { @JoinColumn(name = "ACLID") })
private List<Party> acl;
}

DAO:

    @SuppressWarnings("unchecked")
    @JPAManager(targetEntity = svp.poc.entities.SVPDiscovery.class)
    public class SVPDiscoveryDAO {  

    private EntityManagerFactory emf;

    @PersistenceContext(unitName= "svp-poc")
    EntityManager em ;

    public SVPDiscoveryDAO() {
    }

   private EntityManager getEntityManager() {
    if (emf == null) {
        throw new RuntimeException(
                "The EntityManagerFactory is null.  This must be passed ito the constructor or set using the setEntityManagerFactory() method.");
    }
    return emf.createEntityManager();
    }

    @Action(Action.ACTION_TYPE.CREATE)
    public void addSVPDiscoveryService (SVPDiscovery svpDiscovery){
         em = getEntityManager();
        try {
            em.merge(svpDiscovery);
            em.flush();
        }catch (Exception ex) {
                throw ex;
        }finally {

        }
    }

persistence.xml:

<persistence-unit name="svp-poc" >
<jta-data-source>jdbc/svp-poc-db</jta-data-source> 
<mapping-file>META-INF/named-queries.xml</mapping-file>
    <class>svp.poc.entities.Location</class>
     ........
</persistence-unit>

Then when i persist an instance of SVPDiscovery it gives me the follwoing exception :

Caused by: org.apache.openjpa.persistence.TransactionRequiredException: Can only perform operation while a transaction is active.

User
  • 573
  • 3
  • 15
  • 28
  • and? the message says you haven't got an active transaction, so why not get the transaction and invoke begin() and then commit() after the operation? – Neil Stockton May 13 '15 at 08:33
  • when am doing that it gives me the follwoing exception " You cannot access the EntityTransaction when using managed transactions." – User May 13 '15 at 08:38
  • and where is your managed transaction definition? and what is the stack trace of that exception you post in the question? – Neil Stockton May 13 '15 at 08:40
  • I updated the question with persistence.xml and definition of the entity Manager but i didn't spcify a transaction type – User May 13 '15 at 08:48
  • What is the `@JPAManager` annotation? I am not familiar with it. Can you show the full stack trace of the TransactionRequiredException? Are you running this code on an unmanaged thread? – Brett Kail May 14 '15 at 01:25

2 Answers2

0

I encourage you to read the following in detail: http://docs.oracle.com/javaee/6/tutorial/doc/bncih.html

A solution could look like the following. Please keep in mind that it is not tested and I took some assumptions how your environment could look like.

@Stateless
@TransactionAttribute
public class SomeClass {

    @PeristenceContext
    private EntityManager em;

    @Action(Action.ACTION_TYPE.CREATE)
    public void addSVPDiscoveryService (SVPDiscovery svpDiscovery){
        try {
            em.merge(svpDiscovery);
            em.flush();
        } catch (Exception ex) {
            throw ex;
        } 
    }
}
mh-dev
  • 5,264
  • 4
  • 25
  • 23
  • This does basically mean, you do not have a running ejb container or you use it wrong. Beside that you can interaction with the UserTransaction to do JTA compliant transaction management. But I can't tell you how to get it, because I don't know how your code works. – mh-dev May 13 '15 at 09:23
-1

You don't seem to be starting a transaction (which the error message is telling you). Before the .merge() call:

em.getTransaction().begin();

When you want to commit your changes call:

em.getTransaction().commit();

When you're done with your entity manager close it with

em.close();

(I'd call the close inside your finally block).

Thomas Banks
  • 233
  • 1
  • 7