我有一个带有jpa,ejb,backingbean,jsp的结构。我能够查询数据库并将结果返回到页面,但是当我尝试合并(更新)ejb时,它不起作用。没有错误,但它没有更新值。为了测试我只是硬编码了这些值。如果你能告诉我我在做什么,我真的很感激:
JPA:
package com.ray.adtf.jpa;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the GRIDWEIGHT database table.
*
*/
@Entity
@NamedQueries({
@NamedQuery(name="Gridweight.findAll", query="SELECT g FROM Gridweight g"),
@NamedQuery(name = "Gridweight.findByGridid", query = "SELECT g FROM Gridweight g WHERE g.gridid = :gridid")
})
public class Gridweight implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long gridid;
private String checking;
private String drawing;
private String modeling;
@Column(name="\"RELEASE\"")
private String release;
private String structural;
private String thermal;
public Gridweight() {
}
public long getGridid() {
return this.gridid;
}
public void setGridid(long gridid) {
this.gridid = gridid;
}
public String getChecking() {
return this.checking;
}
public void setChecking(String checking) {
this.checking = checking;
}
public String getDrawing() {
return this.drawing;
}
public void setDrawing(String drawing) {
this.drawing = drawing;
}
public String getModeling() {
return this.modeling;
}
public void setModeling(String modeling) {
this.modeling = modeling;
}
public String getRelease() {
return this.release;
}
public void setRelease(String release) {
this.release = release;
}
public String getStructural() {
return this.structural;
}
public void setStructural(String structural) {
this.structural = structural;
}
public String getThermal() {
return this.thermal;
}
public void setThermal(String thermal) {
this.thermal = thermal;
}
}
EJB:
package com.ray.adtf.ejb;
import com.ray.adtf.jpa.Gridweight;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import java.math.BigInteger;
import java.util.List;
@Stateless
public class GridWeightBean {
@PersistenceContext
private EntityManager em;
public void updWeight(Gridweight gridweight) {
em.merge(gridweight);
}
public Gridweight getGridweight(Integer vgridid) {
return em.createQuery("FROM Gridweight where gridid = "+ vgridid, Gridweight.class).getSingleResult();
}
}
支持bean:
package com.ray.adtf.web;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import com.ray.adtf.ejb.*;
import com.ray.adtf.jpa.Gridweight;
@ManagedBean
public class gridWeight {
@EJB
private GridWeightBean ejb;
public Gridweight getGridWeight( int vgridid) {
Gridweight weight1 = ejb.getGridweight(vgridid);
return weight1;
}
public String updWeight() {
Gridweight gw = ejb.getGridweight(2);
gw.setGridid(2);
gw.setDrawing("99");
ejb.updWeight(gw);
return null;
}
}
谢谢!!!
答案 0 :(得分:0)
你的实例来自哪里?你如何处理PU /代码中的交易?还有更多事情需要检查:
你是如何获得Gridweight的? 合并在某些实体状态中什么都不做:
您还必须正确处理您的交易。在提交包含 merge 调用的事务之前,数据库中的更改是不可靠的。
合并比 persist 花费更多的机器资源,因此如果您只是存储新实例,最好使用 persist 。