OPENJPA坚持不懈

时间:2015-05-13 08:26:10

标签: jpa ejb-3.1 websphere-liberty

我正在使用JPA 2和EJB 3.1

拥有以下实体:

@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>

然后,当我坚持SVPDiscovery的一个实例时,它给了我以下的例外:

  

引起:org.apache.openjpa.persistence.TransactionRequiredException:只能在事务处于活动状态时执行操作。

2 个答案:

答案 0 :(得分:0)

我建议您详细阅读以下内容: http://docs.oracle.com/javaee/6/tutorial/doc/bncih.html

解决方案可能如下所示。请记住,它没有经过测试,我假设你的环境看起来如何。

@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;
        } 
    }
}

答案 1 :(得分:-1)

您似乎没有开始交易(错误消息告诉您)。在.merge()调用之前:

em.getTransaction().begin();

如果您想提交更改,请致电:

em.getTransaction().commit();

当您完成实体经理后,请使用

关闭它
em.close();

(我在你的finally块中调用close)。