当前位置: 代码迷 >> java >> java-无法在数据库表中设置值以反映更改
  详细解决方案

java-无法在数据库表中设置值以反映更改

热度:60   发布时间:2023-07-27 09:47:16.0

我有一个简单的网页,可以单击一个下拉菜单并选择自己喜欢的文档版本。每个文档版本都有其自己的文件状态,以Active(1)Obsolete(2)表示

因此,如果我更改为文档的任何版本,则可以查看该文档的详细信息。在文档旁边有一个``编辑''按钮,该按钮将根据版本的文件状态显示。

一次只能有一个文件处于活动状态。 因此,如果有两个版本,即版本A和版本B,且其中A处于活动状态,则B必须过时。如果我更改为查看版本B(通过下拉菜单),则不应显示“编辑”按钮。

在我的数据库中,我有一列名为fstatus的int表示。 检索文档版本时,将检查该列值。 如果为1,则显示“编辑”按钮,否则不显示该按钮。

我的问题是当我更改为非活动版本时,我希望将值设置为不同于1的值,以便它自动删除按钮。 我似乎无法使其正常工作,因此我需要知道我在做什么错。

Fmedia.java :有一个getter和setter方法,用于获取表中的列并将其值设置为

 public int getFstatus() {
        return fstatus;
    }

    public void setFstatus(int fstatus) {
        this.fstatus = fstatus;
    }

File.java

    public Node get_folder(long fileID) {
     //this line is not that important
   List resList1 = nodeFacade.list_parent(fileID, LinkType.VER_LINKS);

       // This retrieves version document that are inactive
       if(resList1.size()==1){
          // grabs the unique fileID so it knows which row in the table to update the column status
          Fmedia fmedia = new Fmedia(fileID);
          //set the status column to 2 (initially it's 1)
          fmedia.setFstatus(2);
          // by changing the value to 2 before retrieving the record,it should  be able to retrieve the record now and not display the button.
          // returns the records of that inactive version.
          return (Node) resList1.get(0);
        }


    }

我怀疑问题出在fmedia.setFstatus(2); 因为我实际上需要将记录从1更新为2,并且它是表中的现有记录。 另外,我运行了一个调试程序,它能够检索正确的fileID,但是由于某种原因它无法更新状态栏,这意味着它仍然是1,这不是我想要的。

有办法解决这个问题吗?

创建时:

Fmedia fmedia = new Fmedia(fileID);
fmedia.setFstatus(2);

它创建对象并将值分配给状态,但是不会将数据保存/持久保存到数据库中。

您需要隐式调用它以确保EntityManager保存数据。

由于您正在使用:

<groupId>com.htasia</groupId>
<artifactId>PantonMCRE</artifactId>
<version>9.5.0</version>

您需要执行以下操作:

Fmedia fmedia = new Fmedia(fileID);
fmedia.setFstatus(2);
em.refresh(fmedia); // This will run the "UPDATE .... WHERE nodeid = ?"

或使用:

Query createQuery(CriteriaUpdate updateQuery)

例:

CriteriaUpdate<Fmedia> updateCriteria = builder.createCriteriaUpdate(Fmedia.class);
Root<Fmedia> root = updateCriteria.from(Fmedia.class);
updateCriteria.set(Fmedia.fstatus, 2); // make this public from the model
updateCriteria.where(builder.equal(root.get(Fmedia.nodeid), fileID)); // public instead of private
em.createQuery(updateCriteria).executeUpdate();
  相关解决方案