Hibernate的联合主键
一个项目中,数据库的一个表用了联合主键,以前都是用jdbc来做的,现在要改用hibernate来做,在网上找了些资料,都提到联合主键的问题。大家
都不推荐使用联合主键,关键是因为其需要自己手工维护,比较麻烦。数据库已经定好了,由于一些原因没办法更改了,所以只能这样做了。
数据库表:
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: 共两个,TradeSequence 和 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;
}
}
对此表的操作,需求是要根据主键 treasuryBranchCd 和 keyDt 使字段seqNum实现自增。即每次调用时加1.
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("返回的SeqNum="+ts.getSeqNum());
在Hibernate3、tomcat6、JDK1.6环境下测试成功。
数据库表:
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: 共两个,TradeSequence 和 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;
}
}
对此表的操作,需求是要根据主键 treasuryBranchCd 和 keyDt 使字段seqNum实现自增。即每次调用时加1.
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("返回的SeqNum="+ts.getSeqNum());
在Hibernate3、tomcat6、JDK1.6环境下测试成功。