如题
以下是网站找的两段代码,用这个就可以生成主键了?
注解部分看不懂
请问生成出来的主键就是Long id么?
为什么我调用这个类取id是个null?@Entity
public class PK_Sequence implements Serializable { 
@Id
@SequenceGenerator(name="PK_SEQ_TBL",sequenceName="PK_SEQ_NAME") 
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="PK_SEQ_TBL") 
     
    private static final long serialVersionUID = 1L; 
    private Long id;
    
    public void setId(Long id){
     this.id=id;
    }
    public long getId() {
        return id;
     }
}
@Target(value = {ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) 
@Retention(value = RetentionPolicy.RUNTIME) 
public @interface SequenceGenerator {    public String name(); //主键生成策略的名称   public String sequenceName() default ""; //生成策略用到的数据库序列名称   public int initialValue() default 1; //主键初识值   public int allocationSize() default 50;  //每次主键值增加的大小

解决方案 »

  1.   

    @Entity
    public   class   PK_Sequence   implements   Serializable   {  
            private   static   final   long   serialVersionUID   =   1L;          /*我不知道你出了什么问题, 但是明显的一个错误就是注解的实例域错了. 
               你要把下面的注解放置在实例域id上. 而不是像原来的代码那样放在静态域serialVersionUID上.
            */
            @Id
            @SequenceGenerator(name= "PK_SEQ_TBL ",sequenceName= "PK_SEQ_NAME ")  
            @GeneratedValue(strategy   =   GenerationType.SEQUENCE,generator= "PK_SEQ_TBL ")  
            private   Long   id;
           
            public   void   setId(Long   id){
                    this.id=id;
            }
            public   long   getId()   {
                    return   id;
            }
      

  2.   

    改过来了,但是id还是null值
    想问一下sequenceName是数据库内必须存在的么,因为我这个是要做一个组件,并没有连接到数据库
    这样可以生成出id么
    正确生成出来的id是你每调用一次getter方法,返回的id都不一样???
    感谢回复
      

  3.   

    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
    @SequenceGenerator(name = "SEQ_BDINFO", allocationSize = 1, sequenceName = "SEQ_BDINFO")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_BDINFO")
    public Long getId() {
    return this.id;
    }三个红色的地方都是主键序列名称