本文主要介绍了SQL Server数据库中主键以及复合主键的配置,接下来我们就开始介绍。
一般情况下一个表中的主键,id intidentity(1,1)primary key
这是最常见的咯,用注解的形式标记这种主键也很简单
@Id
@GeneratedValue
@Column(name="RecId")
public int getRecId() {
return RecId;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
复合主键个人认为用到的很少,呜呜还是碰到了,由于没有经验,东装西摸,浪费了很长时间才把复合主键配好了,并且还出了很多异常,如下所示:
一個裱中可以有多個字段組成的主鍵
create table EL_TransIdTable(
TableName nvarchar(50) ,
LastTransId nvarchar(15),
Prefix nchar(5),
DomainId nvarchar(10) primary key(TableName,DomainId)
)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
其中TableName 、DomainId两个字段作为此表的主键。
在配置中主要分为两个步骤:
1 为复合主键,建立一个复合主键类,这个类包括两个字段,(有几个字段组成主键 就包含几个字段 )这个复合主键类实现Serializable接口,有public 无参的构造方法 重写equals 和hashcode方法。
2:在实体类里面用idclass标示复合主键类 详情如下:
新建复合主键类TableDomainIdPK.java。
package com.barcode.Model;
import java.io.Serializable;
public class TableNameDomainIdPK implements Serializable{
public TableNameDomainIdPK(){
}
private String TableName;
private String DomainId;
public String getTableName() {
return TableName;
}
public void setTableName(String tableName) {
TableName = tableName;
}
public String getDomainId() {
return DomainId;
}
public void setDomainId(String domainId) {
DomainId = domainId;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result =1;
result=PRIME*result+((TableName==null)?0:TableName.hashCode());
result=PRIME*result+((DomainId==null)?0:DomainId.hashCode());
return result;
}
@Override
public boolean equals(java.lang.Object obj) {
if(this ==obj){
return true;
}
if(null ==obj ){
return false;
}
final TableNameDomainIdPK other=(TableNameDomainIdPK)obj;
if(DomainId==null){
if(other.DomainId!=null){
return false;
}
}else if(!DomainId.equals(other.DomainId)){
return false;
}
if(TableName==null){
if(other.TableName!=null){
return false;
}
}else if (!TableName.equals(other.TableName)){
return false;
}
return true;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
新建实体类EL_TransIdTable.java。
实体类中的配置如下:
package com.barcode.Model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="EL_TransIdTable")
@IdClass(TableNameDomainIdPK.class)
public class EL_TransIdTable implements Serializable {
private String TableName;
private String LastTransId;
private String Prefix;
private String DomainId;
@Id
@Column(name="TableName", nullable = false)
public String getTableName() {
return TableName;
}
public void setTableName(String tableName) {
TableName = tableName;
}
@Column(name="LastTransId")
public String getLastTransId() {
return LastTransId;
}
public void setLastTransId(String lastTransId) {
LastTransId = lastTransId;
}
@Column(name="Prefix")
public String getPrefix() {
return Prefix;
}
public void setPrefix(String prefix) {
Prefix = prefix;
}
@Id
@Column(name="DomainId", nullable = false)
public String getDomainId() {
return DomainId;
}
public void setDomainId(String domainId) {
DomainId = domainId;
}
public void Print_Info(){
System.out.println(this.getDomainId()+this.getLastTransId()+this.getPrefix()+this.getTableName());
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
关于SQL Server数据库复合主键的设置就介绍到这里,希望能够对您有所收获!
【编辑推荐】