如何在Postgres序列中使用hibernate中的主键? 我需要自动生成主要内容,以及如何获取在Hibernate中使用的序列值
这是我的代码
映射类
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
@Entity
@Table(name="ensaio")
public class Ensaio implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ensaio_seq")
@SequenceGenerator(name="ensaio_seq", sequenceName="ensaio_seq")
@Column(name="ensaio_id")
private long id;
private String nome;
@Column(nullable=false)
private String modelo;
private String site;
@Temporal(javax.persistence.TemporalType.DATE)
private Date datadoensaio;
private int qtdeFotos;
private String fotografo;
getter and setters...
}
我的道:
import java.util.List;
import model.Ensaio;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.hibernate.HibernateUtil;
public class HEnsaioDAO {
private Session session;
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
public HEnsaioDAO() {
session = sessionFactory.openSession();
}
public List<Ensaio> findAll(){
Query query = session.createQuery("from Ensaio");
return query.list();
}
public void insertEnsaio(Ensaio ensaio){
session.save(ensaio);
}
//testing insertion
public static void main(String[] args) {
Ensaio e = new Ensaio();
e.setModelo("Roberta Maria");
e.setSite("dsestudio");
e.setFotografo("Jose Sobrero");
e.setNome("Dama das camelias");
HEnsaioDAO dao = new HEnsaioDAO();
dao.insertEnsaio(e);
List<Ensaio> list = dao.findAll();
for (Ensaio ensaio : list) {
System.out.println(ensaio.getModelo());
System.out.println(ensaio.getNome());
System.out.println(ensaio.getFotografo());
}
}
}
数据库中没有任何事情发生(Postgres 8.3)。 怎么了? 感谢