In a project, a database table uses a federated primary key, which was previously made using JDBC. Now we need to use hibernate instead. I found some information on the Internet and mentioned the federated primary key. Joint primary keys are not recommended. The key is that they need to be manually maintained, which is troublesome. The database has been fixed and cannot be changed for some reason.
Database Table:
If exists (select * From DBO. sysobjects where id = object_id ('dbo. frx_trade_sequence ') and type = 'U ')
Drop table [DBO]. [frx_trade_sequence]
Go
Create Table [DBO]. [frx_trade_sequence] (
[Treasury_branch_cd] not null,
[Key_dt] [datetime] not null,
[Seq_num] [seqnum] not null
) On [Default]
Go
Alter table dbo. frx_trade_sequence add primary key (treasury_branch_cd, key_dt)
Go
Model: There are two in total: tradesequence and tradesequenceid.
Tradesequence:
Import javax. Persistence. attributeoverride;
Import javax. Persistence. attributeoverrides;
Import javax. Persistence. column;
Import javax. Persistence. embeddedid;
Import javax. Persistence. entity;
Import javax. Persistence. Table;
/**
* Frxtradesequence generated by hbm2java
*/
@ Entity
@ Table (name = "frx_trade_sequence ")
Public class tradesequence implements java. Io. serializable {
Private tradesequenceid ID;
Private integer seqnum;
Public tradesequence (){
}
Public tradesequence (tradesequenceid ID, integer seqnum ){
This. ID = ID;
This. seqnum = seqnum;
}
@ Embeddedid
@ Attributeoverrides ({
@ Attributeoverride (name = "treasurybranchcd", column = @ column (name = "treasury_branch_cd", nullable = false, length = 12 )),
@ Attributeoverride (name = "keydt", column = @ column (name = "key_dt", nullable = false, length = 23 ))})
Public tradesequenceid GETID (){
Return this. ID;
}
Public void setid (tradesequenceid ID ){
This. ID = ID;
}
@ Column (name = "seq_num", nullable = false)
Public integer getseqnum (){
Return this. seqnum;
}
Public void setseqnum (integer seqnum ){
This. seqnum = seqnum;
}
}
Tradesequenceid:
Import java. util. date;
Import javax. Persistence. column;
Import javax. Persistence. embeddable;
/**
* Frxtradesequenceid generated by hbm2java
*/
@ Embeddable
Public class tradesequenceid implements java. Io. serializable {
Private string treasurybranchcd;
Private date keydt;
Public tradesequenceid (){
}
Public tradesequenceid (string treasurybranchcd, date keydt ){
This. treasurybranchcd = treasurybranchcd;
This. keydt = keydt;
}
@ Column (name = "treasury_branch_cd", nullable = false, length = 12)
Public String gettreasurybranchcd (){
Return this. treasurybranchcd;
}
Public void settreasurybranchcd (string treasurybranchcd ){
This. treasurybranchcd = treasurybranchcd;
}
@ Column (name = "key_dt", nullable = false, length = 23)
Public date getkeydt (){
Return this. keydt;
}
Public void setkeydt (date keydt ){
This. keydt = keydt;
}
}
The operation on this table requires that the field seqnum be auto-incrementing based on the primary key treasurybranchcd and keydt. Add 1 for each call.
Tradesequenceid tid = new tradesequenceid ();
TID. setkeydt (keydate );
TID. settreasurybranchcd (treasurybranchcd );
Tradesequence Ts = new tradesequence ();
TS. setid (TID );
Detachedcriteria criteria = detachedcriteria. forclass (tradesequence. Class );
Criteria. Add (restrictions. ideq (TID ));
List list = manager. findbycriteria (criteria );
If (list! = NULL & list. Size ()> 0)
{
TS = (tradesequence) (list. Get (0 ));
TS. setseqnum (TS. getseqnum () + 1 );
}
Else
{
TS. setseqnum (New INTEGER (1 ));
}
Manager. Save (TS );
System. Out. println ("Returned seqnum =" + ts. getseqnum ());
The test is successful in hibernate3, tomcat6, and jdk1.6 environments.