如何使用休眠自动生成其他序列?

时间:2015-04-02 14:14:51

标签: java hibernate postgresql

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;

此注释将在postgresql中创建以下序列:

CREATE SEQUENCE hibernate_sequence
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 0
  CACHE 1;
ALTER TABLE hibernate_sequence
  OWNER TO postgres;

如何以休眠自动生成附加序列的方式注释id字段,然后使用此序列仅增加此特定表中的行?

1 个答案:

答案 0 :(得分:1)

You need create a sequence and follow this way

/**
 * The persistent class for the TB_FINALIDADE_PORTARIA database table.
 * 
 */
@Entity
@Table(name="TB_FINALIDADE_PORTARIA")
public class FinalidadePortaria implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="FINALIDADE_PORTARIA_ID_GENERATOR", sequenceName="SQ_FINALIDADE_PORTARIA")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="FINALIDADE_PORTARIA_ID_GENERATOR")
    @Column(name="ID_FINALIDADE_PORTARIA")
    private Long id;