Combined primary key of Hibernate

Source: Internet
Author: User
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]
Create Table [DBO]. [frx_trade_sequence] (
[Treasury_branch_cd] not null,
[Key_dt] [datetime] not null,
[Seq_num] [seqnum] not null
) On [Default]


Alter table dbo. frx_trade_sequence add primary key (treasury_branch_cd, key_dt)


Model: There are two in total: tradesequence and tradesequenceid.


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;



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 );
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.