有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate保存后插入

在我意识到我有

public void insert(String type )
    Session s= dao.getCurrentSession();
    Long id=dao.getId();
    s.createQuery("insert into table1 t (id,msg,(select t2.typeId from table2 t2 where type =?)) VALUES (?,?,?)")
        .setParameter(0, id,       Hibernate.Long)
        .setParameter(1, "msg",    Hibernate.String)
        .setParameter(2, type,     Hibernate.String)
        .executeUpdate();
}

我想换成

public void insert(String type)
    Session s= dao.getCurrentSession();
    Long id=dao.getId();
    Long typeId=null;
    typeId=(Long)s.createQuery("select t2.type from table2 t2 where type =?").
            .setParameter(0, type,  Hibernate.String)
            .uniqueResult();

    if(typeId==null){
       Table2 t=new Table2(type);
       typeId=(Long)s.save(t);
    }
    s.createQuery("insert into table1 t (id,msg,Table2")) VALUES (?,?,?)
        .setParameter(0, id,      Hibernate.Long)
        .setParameter(1, "msg",   Hibernate.String)
        .setParameter(2, typeId,  Hibernate.Long)
        .executeUpdate();
}

在那次操作之后,我得到了关于约束的异常,因为我没有在table2中使用typeId新建type,然而,在IDEA的调试模式下,我可以看到,typeId=s.save(t);从insert返回新的id。我的id在oracle中按顺序生成

如果我添加Transaction tx = s.beginTransaction()tx.commit(),那么我会得到新的异常,该会话将关闭,并且无法进行插入

如何避免这种情况

附言-----------------

我的桌子是

 public class Table2 implements Serializable {

  private static final long serialVersionUID = 6213740208940441112L;
  private Long typeId = null;
  private String type;

  public Table2 () {
  }

  public Table2 (String type) {
  this.type=type;
  }

  @Id
  @SequenceGenerator( name = "table_type", sequenceName = "seq_table_type", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "table_type")
  public Long getTypeId() {
    return this.typeId;
  }
  public void setId(Long id) {
    this.id = id;
  }

  @Column(name = "type", nullable = false)
  @NotNull
  public String getType() {
    return this.type;
  }

  public void setType(String type) {
    this.type= type;
  }

共 (2) 个答案

  1. # 1 楼答案

    在我看来,您似乎更改了Long-named-typeId的值,但在insert中,您仍然使用名为type的传入字符串属性

    尝试更改传入类型或在插入中使用创建的typeId

  2. # 2 楼答案

    我发现了一些认识

    public void insert(String type)
        Session s= dao.getCurrentSession();
        Long id=dao.getId();
        Long typeId=(Long)s.createQuery("select typeId from table2 where type =?").
                .setParameter(0, type,  Hibernate.String)
                .uniqueResult();
    
        if(typeId==null){
           Table2 t=new Table2(type);
           typeId=(Long)s.save(t);
           s.flush();
        }
        s.createQuery("insert into table1 t (id,msg,Table2")) VALUES (?,?,?)
            .setParameter(0, id,      Hibernate.Long)
            .setParameter(1, "msg",   Hibernate.String)
            .setParameter(2, typeId,  Hibernate.Long)
            .executeUpdate();
            s.flush();
    }
    

    但是现在,即使我做了这样的事情,如果我遇到任何异常,我也无法回滚任何更改

     ...
    } catch (Throwable e) {
            s.getTransaction().rollback();
    }
    

    这是因为Oracle的序列无法进行回滚