自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SQL Server數(shù)據(jù)庫主鍵及復(fù)合主鍵的配置

數(shù)據(jù)庫 SQL Server
本文主要介紹了SQL Server數(shù)據(jù)庫中主鍵以及復(fù)合主鍵的一些配置問題,希望能對(duì)您有所幫助。

本文主要介紹了SQL Server數(shù)據(jù)庫中主鍵以及復(fù)合主鍵的配置,接下來我們就開始介紹。

一般情況下一個(gè)表中的主鍵,id intidentity(1,1)primary key

這是最常見的咯,用注解的形式標(biāo)記這種主鍵也很簡單

 

  1. @Id    
  2.  
  3. @GeneratedValue    
  4.  
  5. @Column(name="RecId")    
  6.  
  7. public int getRecId() {    
  8.  
  9. return RecId;    
  10.  
  11. }   

 

復(fù)合主鍵個(gè)人認(rèn)為用到的很少,嗚嗚還是碰到了,由于沒有經(jīng)驗(yàn),東裝西摸,浪費(fèi)了很長時(shí)間才把復(fù)合主鍵配好了,并且還出了很多異常,如下所示:

一個(gè)裱中可以有多個(gè)字段組成的主鍵  

  1. create table EL_TransIdTable(    
  2.  
  3. TableName nvarchar(50) ,    
  4.  
  5. LastTransId nvarchar(15),    
  6.  
  7. Prefix nchar(5),    
  8.  
  9. DomainId nvarchar(10) primary key(TableName,DomainId)    
  10.  
  11. )   

 

其中TableName 、DomainId兩個(gè)字段作為此表的主鍵。

在配置中主要分為兩個(gè)步驟:

1 為復(fù)合主鍵,建立一個(gè)復(fù)合主鍵類,這個(gè)類包括兩個(gè)字段,(有幾個(gè)字段組成主鍵 就包含幾個(gè)字段 )這個(gè)復(fù)合主鍵類實(shí)現(xiàn)Serializable接口,有public 無參的構(gòu)造方法 重寫equals 和hashcode方法。

2:在實(shí)體類里面用idclass標(biāo)示復(fù)合主鍵類 詳情如下:

新建復(fù)合主鍵類TableDomainIdPK.java。

 

  1. package com.barcode.Model;    
  2.  
  3. import java.io.Serializable;    
  4.  
  5. public class TableNameDomainIdPK implements Serializable{    
  6.  
  7. public TableNameDomainIdPK(){    
  8.  
  9. }    
  10.  
  11. private String TableName;    
  12.  
  13. private String DomainId;    
  14.  
  15. public String getTableName() {    
  16.  
  17. return TableName;    
  18.  
  19. }    
  20.  
  21. public void setTableName(String tableName) {    
  22.  
  23. TableName = tableName;    
  24.  
  25. }    
  26.  
  27. public String getDomainId() {    
  28.  
  29. return DomainId;    
  30.  
  31. }    
  32.  
  33. public void setDomainId(String domainId) {    
  34.  
  35. DomainId = domainId;    
  36.  
  37. }    
  38.  
  39. @Override    
  40.  
  41. public int hashCode() {    
  42.  
  43. final int PRIME = 31;    
  44.  
  45. int result =1;    
  46.  
  47. result=PRIME*result+((TableName==null)?0:TableName.hashCode());    
  48.  
  49. result=PRIME*result+((DomainId==null)?0:DomainId.hashCode());    
  50.  
  51. return result;    
  52.  
  53. }    
  54.  
  55. @Override    
  56.  
  57. public boolean equals(java.lang.Object obj) {    
  58.  
  59. if(this ==obj){    
  60.  
  61. return true;    
  62.  
  63. }    
  64.  
  65. if(null ==obj ){    
  66.  
  67. return false;    
  68.  
  69. }    
  70.  
  71. final TableNameDomainIdPK other=(TableNameDomainIdPK)obj;    
  72.  
  73. if(DomainId==null){    
  74.  
  75. if(other.DomainId!=null){    
  76.  
  77. return false;    
  78.  
  79. }    
  80.  
  81. }else if(!DomainId.equals(other.DomainId)){    
  82.  
  83. return false;    
  84.  
  85. }    
  86.  
  87. if(TableName==null){    
  88.  
  89. if(other.TableName!=null){    
  90.  
  91. return false;    
  92.  
  93. }    
  94.  
  95. }else if (!TableName.equals(other.TableName)){    
  96.  
  97. return false;    
  98.  
  99. }           
  100.  
  101. return true;    
  102.  
  103. }    
  104.  
  105. }   

 

新建實(shí)體類EL_TransIdTable.java。

實(shí)體類中的配置如下:

 

  1. package com.barcode.Model;    
  2.  
  3. import java.io.Serializable;    
  4.  
  5. import javax.persistence.Column;    
  6.  
  7. import javax.persistence.Entity;    
  8.  
  9. import javax.persistence.Id;    
  10.  
  11. import javax.persistence.IdClass;    
  12.  
  13. import javax.persistence.Table;    
  14.  
  15. @Entity       
  16.  
  17. @Table(name="EL_TransIdTable")    
  18.  
  19. @IdClass(TableNameDomainIdPK.class)    
  20.  
  21. public class EL_TransIdTable implements Serializable {    
  22.  
  23. private String TableName;    
  24.  
  25. private String LastTransId;    
  26.  
  27. private String Prefix;    
  28.  
  29. private String DomainId;    
  30.  
  31. @Id    
  32.  
  33. @Column(name="TableName"nullable = false)    
  34.  
  35. public String getTableName() {    
  36.  
  37. return TableName;    
  38.  
  39. }    
  40.  
  41. public void setTableName(String tableName) {    
  42.  
  43. TableName = tableName;    
  44.  
  45. }       
  46.  
  47. @Column(name="LastTransId")    
  48.  
  49. public String getLastTransId() {    
  50.  
  51. return LastTransId;    
  52.  
  53. }    
  54.  
  55. public void setLastTransId(String lastTransId) {    
  56.  
  57. LastTransId = lastTransId;    
  58.  
  59. }    
  60.  
  61. @Column(name="Prefix")    
  62.  
  63. public String getPrefix() {    
  64.  
  65. return Prefix;    
  66.  
  67. }    
  68.  
  69. public void setPrefix(String prefix) {    
  70.  
  71. Prefix = prefix;    
  72.  
  73. }       
  74.  
  75. @Id    
  76.  
  77. @Column(name="DomainId"nullable = false)    
  78.  
  79. public String getDomainId() {    
  80.  
  81. return DomainId;    
  82.  
  83. }    
  84.  
  85. public void setDomainId(String domainId) {    
  86.  
  87. DomainId = domainId;    
  88.  
  89. }    
  90.  
  91. public void Print_Info(){    
  92.  
  93. System.out.println(this.getDomainId()+this.getLastTransId()+this.getPrefix()+this.getTableName());    
  94.  
  95. }    
  96.  
  97. }   

 

關(guān)于SQL Server數(shù)據(jù)庫復(fù)合主鍵的設(shè)置就介紹到這里,希望能夠?qū)δ兴斋@!

【編輯推薦】

  1. 用mysqldumpslow分析執(zhí)行較慢的SQL語句
  2. 一些很實(shí)用的Oracle數(shù)據(jù)庫優(yōu)化策略總結(jié)篇
  3. 使用MySQL Proxy告終讀寫離別的操作實(shí)例
  4. 在SQL觸發(fā)器或存儲(chǔ)過程中獲取登錄用戶信息
  5. 局域網(wǎng)所有機(jī)器都能連接MySQL數(shù)據(jù)庫的設(shè)置命令
責(zé)任編輯:趙鵬 來源: CSDN博客
相關(guān)推薦

2011-03-28 14:29:46

SQL Server數(shù)主鍵列

2011-08-03 10:04:57

SQL Server數(shù)沒有主鍵的表

2011-08-01 09:50:31

SQL Server數(shù)主鍵索引

2010-10-21 14:54:32

查詢SQL Serve

2012-02-03 10:07:04

HibernateJava

2010-09-25 10:05:25

sql server主

2010-10-19 17:21:35

SQL SERVER主

2010-09-25 09:45:46

sql server主

2010-10-20 10:19:33

sql server刪

2011-05-12 13:34:57

SQL Server

2010-09-25 09:55:14

sql server主

2010-07-05 15:12:30

SQL Server主

2010-10-19 17:34:10

sql server主

2011-04-13 14:20:52

SQL Server主鍵

2010-10-20 10:31:57

sql server聯(lián)

2010-10-26 15:54:02

連接oracle數(shù)據(jù)庫

2010-09-01 16:44:26

SQL刪除主鍵

2009-06-01 12:11:31

hibernatejpa復(fù)合主鍵

2019-08-29 07:13:50

oracle數(shù)據(jù)庫主鍵

2010-06-02 11:24:57

MySQL數(shù)據(jù)庫主鍵
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)