@Stateless webservice with JPA + JTA:如何提交托管实体的更改?

时间:2013-04-12 07:28:35

标签: java jpa ejb jax-rs jta

我有以下简单的webservice声明为@Stateless EJB在GlassFish 3.1.2.2上运行,EclipseLink 2.4.1使用JTA DataSource连接到MySQL数据库:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response update(TimeRow e) throws Exception {
    if ((e.getKey() == null) || !e.getKey().keyValid()) {
        return Response.status(400).build();
    }

    TimeRow existing = em.find(TimeRow.class, e.getKey());
    if (existing == null) {
        em.persist(e);
    } else {
        existing.setValues(e.getValues());
        em.flush();
    }
    return Response.status(204).build();
}

TimeRow的实体类:

@Entity
@NamedQueries({
    @NamedQuery(name="TimeRow.findAllByUser",
         query="SELECT t FROM TimeRow t WHERE t.table.userId = :uid")
})
public class TimeRow implements Serializable {

    @EmbeddedId
    private TimeRowPK key;

    @MapsId("userId")
    @JoinColumn(name = "USERID", referencedColumnName = "userId")
    @ManyToOne
    private UserTable table;
    @Column(name="ROWVALUES")
    private List<Double> values;

    public TimeRow() {
        this.key = new TimeRowPK();
        this.values = new ArrayList<Double>(20);
        extendValuesTo20();
    }

    public TimeRow(String uid, Date date) {
        this.key = new TimeRowPK(date, uid);
        this.table = new UserTable(uid);
        this.values = new ArrayList<Double>(20);
        extendValuesTo20();
    }

    public List<Double> getValues() {
        return values;
    }

    public void setValues(List<Double> values) {
        this.values = values;
        extendValuesTo20();
    }

    private void extendValuesTo20() {
        if (this.values.size() < 20) {
             for (int i = this.values.size(); i < 20; i++) {
                  this.values.add(0.0);
             }
        }
    }

}

@EmbeddableId TimeRowPK

@Embeddable
public class TimeRowPK implements Serializable {

    public TimeRowPK() { }

    public TimeRowPK(Date date, String userId) {
        this.date = date;
        this.userId = userId;
    }

    @Column(name="DAY")
    private Date date;
    @Column(name = "USERID")
    private String userId;

    public boolean keyValid() {
        return ((date != null) && ((userId != null) && !userId.isEmpty()));
    }

}

persistence.xml(没有<persistence>标记):

<persistence-unit name="test" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/test</jta-data-source>
    <class>com.test.TimeRow</class>
    <class>com.test.TimeRowPK</class>
    <class>...</class>
    <properties>
        <property name="eclipselink.ddl-generation" value="create-tables" />
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
</persistence-unit>

Webservice声明:

@Path("row")
@Stateless
public class TimeRowWebService {

    @PersistenceContext(unitName = "test")
    private EntityManager em;

    ...

}

问题在于,如果实体存在,则更改仅存储在PersistenceContext中,但它们不会提交到数据库。这意味着我可以使用更改检索正确的数据,但是例如,如果我重新启动AS,则更改将消失。 AS日志中没有错误。

所以我想我必须做一些bean级别的手动事务处理来完成这项工作。我究竟需要添加什么才能使其正常工作?

2 个答案:

答案 0 :(得分:2)

在努力以@EmbeddedId的方式运行之后,我尝试使用生成的@Id来实现它,然后为这两个关键字段添加唯一约束。

<强>实体

public class TimeRow implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @JoinColumn(name = "USERID", referencedColumnName = "USERID")
    @ManyToOne(optional = false)
    private UserTable table;

    @Basic(optional = false)
    @Column(name = "DAY")
    @Temporal(TemporalType.DATE)
    private Date date;

    @Lob
    @Column(name="ROWVALUES")
    private List<Double> values;

    ...
}

此外,我已将数据库引擎从MyISAM更改为InnoDB,以获得更好的外键支持。为此,我在default-storage-engine = innodb部分下添加了/etc/mysql/my.cnf[mysqld]

使用DDL生成数据库结构后,我USERIDDAY added a unique constraint

alter table TIMEROW add unique index(USERID,DAY);

现在它正常工作并正确修改数据:)非常感谢所有为此问题做出贡献的人!

答案 1 :(得分:1)

也许抓到了:

public void setValues(List<Double> values) {
    this.values = values;
    extendValuesTo20();
}

尝试将其更改为使用容器列表,如下所示:

public void setValues(List<Double> values) {
    this.values.clear();
    this.values.addAll(values);
    extendValuesTo20();
}

此外,这是一个非常奇怪的JPA使用。我没见过没有@ElementCollection

的列表