ejb没有合并到db

时间:2015-09-18 00:07:13

标签: jpa sql-update ejb

我有一个带有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;
    }    
}

谢谢!!!

1 个答案:

答案 0 :(得分:0)

你的实例来自哪里?你如何处理PU /代码中的交易?还有更多事情需要检查:

你是如何获得Gridweight的? 合并在某些实体状态中什么都不做:

  • 如果实体已经在持久化上下文(会话)中,则不执行任何操作,级联除外
  • 如果实体已分离,则返回附加(管理)
  • 的副本(对象)
  • 如果实体是瞬态的(新实例),则保存该实体并返回持久(和托管)副本
  • 如果实体已分离,但当前实体管理器中存在具有相同标识符的对象,则分离对象的状态将复制到当前持久实体中,并返回它(当前)

您还必须正确处理您的交易。在提交包含 merge 调用的事务之前,数据库中的更改是不可靠的。

合并 persist 花费更多的机器资源,因此如果您只是存储新实例,最好使用 persist