【问题标题】:ejb not merging to dbejb没有合并到数据库
【发布时间】:2015-09-18 00:07:12
【问题描述】:

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

谢谢!!!

【问题讨论】:

  • 不确定它没有显示 jsp 代码,但它是:
  • 能否提供 backingbean 代码?
  • 不确定为什么 backing bean 没有发布......任何帮助都会非常感谢您

标签: jpa sql-update ejb


【解决方案1】:

您的实例来自哪里?您如何处理 PU/代码中的事务?还有更多需要检查的地方:

您是如何获得 Gridweight 的? Merge 在某些实体状态中根本不做任何事情:

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

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

Mergepersist 消耗更多的机器资源,所以如果您只是存储新实例,最好使用 persist

【讨论】:

  • 嗨,你能再看看代码吗?由于某种原因,当我发布问题时,支持 bean 代码已被删除......它现在应该在那里,这将回答您在上面发布的大多数问题。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2013-05-06
  • 2015-09-05
相关资源
最近更新 更多