jpa 非主键字段如何配置为自动增长类型(困扰多时的问题)

livan1038 livan1038 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
比如我有一个表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)

先谢谢大家了。
--------------------------------------------------------------------------------
我目前这样写的,但是有问题,不知道这个ID如何写才是正确的写法
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;
}
..................................
}
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
livan1038
Eclipse 58,439 成员 49,457 内容