使用JPA和自动生成的主键标识将实体持久保存到数据库 [英] Persisting entity to database with JPA and autogenerated primary key identity

查看:226
本文介绍了使用JPA和自动生成的主键标识将实体持久保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有表示层和数据库的完整Java EE Web应用程序。我正在使用带有glassfish 3.1和JPA的derby来处理持久性。我创建了一个Read ok但是现在我正在做一个创建并持久保存到数据库的麻烦。我认为我很接近,但是我正在努力创造这种方式,这是不对的。

I have a full Java EE web application with a presentation layer and a database. I'm using derby with glassfish 3.1 and JPA to handle persistence. I've created a Read ok but now I'm having troulbe doing a Create and persisting to the database. I think I'm close but something is not right with the way I'm trying to do the create.

这是我的EAO代码:

/**

 * Session Bean implementation class XRSSeao

 */

@Stateless

@LocalBean

public class XRSSeao {



@PersistenceContext

EntityManager em;

public XRSSeao() {}




    public void addEvent(String update){

    Feed feed = new Feed();

    feed.setStatus(update);

    feed.setId(2);

        em.getTransaction().begin();

            em.persist(feed);

            em.flush();

            em.clear();

        em.getTransaction().commit();

}

}

这将是从另一个EJB调用。我也不想设置ID,因为每当我调用persist方法时,这就是我想要生成的主键。我测试它时得到的错误是:

This will be called from another EJB. I also don't want to have to set the ID since that is the primary key I want that generated whenever I call the persist method. The error I get when I test it is:

引起:java.lang.IllegalStateException:
异常说明:使用JTA时不能使用EntityTransaction。

"Caused by: java.lang.IllegalStateException: Exception Description: Cannot use an EntityTransaction while using JTA."

如果您不知道此代码的问题是什么,但可以提供一个简单的使用自动生成的主键持久化的示例,这将同样有用。

If you don't know what the problem is with this code but can provide an example of simple persisting with autogenerated primary key that would be just as helpful.

这是我正在使用的读取方法:

This is my read method that is working:

public String lastUpdate(){
    String resultString;
    Query q = em.createQuery("SELECT x FROM Feed x WHERE x.id = 1");
    List<Feed> ListofStatus =  q.getResultList();  //alternatively you can use getResultList() for non 1 object is expected. 
    Feed returnStatusObject = ListofStatus.get(0);
    resultString = returnStatusObject.getStatus();
    return resultString;

}

如果我不这样做需要使用Transaction()我还没有找到一个不用于创建的在线示例。

If I don't need to use Transaction() I haven't found an example online that does not use it for create.

推荐答案

您正在使用EJB / JTA与 transaction-type =JTA。然后容器将管理事务本身。您可以通过 @TransactionAttribute <来控制交易/ code> @TransactionAttributeType EJB类/方法的注释(默认情况下不应该这样)。你读过的教程显然没有使用EJB / JTA,只是使用 transaction-type =RESOURCE_LOCAL的应用程序管理的事务。您应该阅读针对EJB / JTA使用的JPA教程。

You're using EJB/JTA with transaction-type="JTA". The container will then manage the transactions itself. You can control the transactions by @TransactionAttribute and @TransactionAttributeType annotations on the EJB class/methods (which by default should however not be necessary). The tutorials which you've read apparently did not use EJB/JTA, but just application managed transactions with transaction-type="RESOURCE_LOCAL". You should read JPA tutorials which are targeted on use with EJB/JTA.

修复您的问题 - 我假设您要继续使用EJB / JTA-,替换

To fix your problem -I assume that you want to keep using EJB/JTA-, replace

em.getTransaction().begin();
em.persist(feed);
em.flush();
em.clear();
em.getTransaction().commit();

by

em.persist(feed);



参见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆