gpt4 book ai didi

java - Hibernate 持久错误,id 字段为空

转载 作者:行者123 更新时间:2023-12-01 12:39:21 25 4
gpt4 key购买 nike

嗨,我正在尝试进行持久化,但看起来好像从未设置过其中一个属性,但是,在进行持久化之前,我打印了值并进行了设置。我正在与 postgrest 合作

我的坚持方法

  public void saveText(Document document){

sessionFactory = new Configuration().configure().buildSessionFactory();

Session session = sessionFactory.getCurrentSession();

System.out.println(document.getText());
System.out.println(document.getId());

try {
session.beginTransaction();
session.persist(document);
session.getTransaction().commit();
}
catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
}

我反对坚持下去

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import br.com.symsar.sysged.acessobd.AccesData;

@Entity
@ManagedBean
@SessionScoped
@Table(name = "documents", schema = "public")
public class Document implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "\"id\"")
private int id;
@Column(name = "\"text\"")
private String text;

public void persist() {
AccesData access = new AccesData();
access.saveText(this);
}

/**
* @return the text
*/
public String getText() {
return text;
}

/**
* @param text
* the text to set
*/
public void setText(String text) {
this.text = text;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

}

hibernate显示的Sql查询

Hibernate: insert into public.documents ("id", "text") values (null, ?)

这是错误:

ERROR: ERRO: Null value in the column "id" violates not-null constraint

最佳答案

字段id在实体类中有JPA注释@Id。这意味着它不能为 null。尝试插入不带 id 参数的值:

Hibernate: insert into public.documents ("text") values (?)

关于java - Hibernate 持久错误,id 字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25263565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com