比如我有一个表cust有以下字段:
-------------------------------
列名    数据类型     允许NULL  主键
id      int          no        自动增长
custId  int          no        yes
name    varchar(50)  no        yes
address varchar(100) yes       no--------------------------
custId和name为主键,但我又想id这个字段设为自动增长类型,这种情况下jpa应该如何写才可以让这个非主键id字段为自动增长类型的呢?(我的数据库是sybase)先谢谢大家了。

解决方案 »

  1.   

    custPk.java
    ----------------------------------
    import java.io.Serializable;
    public class CustPk implements Serializable { private static final long serialVersionUID = -7310844877900844920L;
            private int custId;
            private String name;        public int getCustId(){
                  return this.custId;
            } public String getName() {
    return this.name;
            } public CustPk() {
    } @Override
    public int hashCode() {
    .................
            } @Override
    public boolean equals(Object obj) {
            ........
    }
    }
    --------------------------------------------------------------------
    Cust.java
    --------------------------------------------------------------------
    @Audited
    @Entity
    @Table(name = "Cust")
    @IdClass(CustPk.class)
    public class Cust
    {    private static final long serialVersionUID = -3186455049080602773L;
        Long id;
        int custId;
        String name;
        String address;    public Cust() {
            
        }    @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = "ID", insertable=false, updatable=false, precision=19, scale=0, nullable=false)
        public Long getId() {
            return super.getId();
        }    @Id    
        @Column(name = "custId", length = 10, nullable=false)
        public int getCustId() {
            return this.custId    
        }
        public void setCustid(int custId){
            this.custId = custId;
        }    @Id
        @Column(name = "name", length = 50, nullable=false)
        public String getName() {
            return this.name;
        }    public void setName(String name){
            this.name = name;
        }
    ..................................
    }我目前大概这样写的,但是有问题
      

  2.   

    id int no 自动增长
    custId int no yes
    这2个有重复之嫌
      

  3.   

    我是举一个例子而已,我custId的实际上是一个String,并不是一个int
      

  4.   

    那你为什么 要把2个String 类型的字段设为联合主键
    id 设为主键 custId和name建立唯一约束就行了 
    如果不能改表结构  哪就在程序里自己控制吧
      

  5.   

    你说的这样方法其实就是我目前采用的方法,但我现在的情况是跨数据库操作,如A数据有cust表,B数据库有cust表,当两个表中的ID有相同的资料时就会有问题,相同的ID就是造成后面的资料盖掉前面的资料,变成了两笔完全相同的资料。而我是采用根据ID的排序来做分页,所以才造成了现在这种奇怪的需求。
      

  6.   

    to zn85600301:
    说少了一点,我的资料是把A数据库的cust表和B数据库的cust表union起来,得到最终的结果。因为分页问题,所以才没有在程式里控制。
      

  7.   

    把哪个custId单独一个表 然后关联
    不建议这么做
      

  8.   

    从A库取出的编号前强行加上类似“A_”前缀
    从B库取出的编号前强行加上类似“B_”前缀
    这样就不重复了,呵呵~
      

  9.   

    先存。再改Integer id = session.save(xxx);
    xxx.setYYY(id);
    session.update(xxx);
      

  10.   


    那要不加这样:
    从A库取出的编号强行加上类似“_A”
    从B库取出的编号强行加上类似“_B”缀要不排序就不按编号来排,例如按录入时间来排
      

  11.   

    呵呵,自己解决,和大家分享一下:
    @GeneratedValue(strategy = GenerationType.AUTO) 
        @Column(name = "ID",nullable=false,insertable=false,updatable=false,columnDefinition="numeric(19,0) IDENTITY")
        public Long getId() {
            return super.getId();
        }