嗨我试图做一个持久但看起来如果其中一个属性永远不会被设置但是,就在持续之前我打印值并设置。我正在使用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
答案 0 :(得分:1)
字段id
在您的实体类中包含JPA注释@Id
。这意味着它不能是null
。尝试不带id参数的插入值:
Hibernate: insert into public.documents ("text") values (?)
答案 1 :(得分:0)
尝试删除注释 @ManagedBean @SessionScoped
答案 2 :(得分:0)
我决定不使用会话工厂现在我正在使用实体管理器,当我做这个改变时。 谢谢大家的想法