如何使用Annotations在Hibernate中表示复合键?

时间:2011-08-16 15:16:51

标签: java hibernate persistence

所以我从我的db中反向设计了一些表,当我尝试将对象保存到db时,我得到以下错误:

初始SessionFactory创建failed.org.hibernate.AnnotationException:从com.mycode.Account引用com.mycode.Block的外键具有错误的列数。应该是2 线程“main”java.lang.ExceptionInInitializerError

中的异常

Domain objects Are Block包含许多帐户对象:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "Block")
public Set<EAccount> getAccounts() {
    return this.Accounts;
}

帐户具有ID和角色的复合键。这已在单独的类中设置:

@Embeddable
public class BlockAccountId implements java.io.Serializable {

private long blockOid;
private String accountRole;

public BlockAccountId() {
}

public BlockAccountId(long blockOid, String accountRole) {
    this.blockOid = blockOid;
    this.accountRole = accountRole;
}

@Column(name = "BLOCK_OID", nullable = false)
public long getBlockOid() {
    return this.blockOid;
}

public void setBlockOid(long blockOid) {
    this.blockOid = blockOid;
}

@Column(name = "ACCOUNT_ROLE", nullable = false, length = 10)
public String getAccountRole() {
    return this.accountRole;
}

public void setAccountRole(String accountRole) {
    this.accountRole = accountRole;
}

所以我想知道。如何在blockOid上链接表块和帐户,但仍然确保帐户表具有blockOid和accountRole作为组合键。

非常感谢任何例子!

N.B这是一个Block(One)到Account(Many)的关系。

由于

2 个答案:

答案 0 :(得分:4)

最简单的方法是将关联直接放在嵌入式id组件中。

  

Hibernate参考文档

     

示例(仅编写重要的getter()和setter())

@Entity
public class Block {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="BLOCK_OID")
    long blockOid;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "id.block", cascade=CascadeType.ALL)
    Set<Account> accounts = new HashSet<Account>();
}

@Entity
public class Account {

    @EmbeddedId BlockAccountId id;

    public Account()
    {
        this.id = new BlockAccountId();
    }

    public void setBlock(Block pBlock) {        
        this.id.setBlock(pBlock);
    }

    public Block getBlock() {
        return this.id.getBlock();
    }

    public String getAccountRole() {    
        return this.id.getAccountRole();
    }

    public void setAccountRole(String accountRole) { 
        this.id.setAccountRole(accountRole);
    }
}


@Embeddable
public class BlockAccountId implements java.io.Serializable {

    @ManyToOne(optional = false)    
    private Block block;

    @Column(name = "ACCOUNT_ROLE", nullable = false, length = 10)
    private String accountRole;

    public BlockAccountId() {

    }

    //Implement equals and hashcode
}

相应的数据库表是:

CREATE TABLE  block (
  BLOCK_OID bigint(20) NOT NULL auto_increment,
  PRIMARY KEY  (`BLOCK_OID`)
) 


CREATE TABLE  account (
  ACCOUNT_ROLE varchar(10) NOT NULL,
  block_BLOCK_OID bigint(20) NOT NULL,
  PRIMARY KEY  (`ACCOUNT_ROLE`,`block_BLOCK_OID`),
  KEY `FK_block_OID` (`block_BLOCK_OID`),
  CONSTRAINT `FK_block_OID` FOREIGN KEY (`block_BLOCK_OID`) REFERENCES `block` (`BLOCK_OID`)
)

答案 1 :(得分:0)

基于hibernate文档,这里是link

基于此,您可以执行以下操作:

@Entity 公共类帐户{

@EmbeddedId BlockAccountId id;

@MapsId(value = "blockOid")
@ManyToOne
private Block block;

public Account()
{
    this.id = new BlockAccountId();
}

public void setBlock(Block pBlock) {        
    this.block = pBlock;
}

public Block getBlock() {
    return this.block;
}

public String getAccountRole() {    
    return this.id.getAccountRole();
}

public void setAccountRole(String accountRole) { 
    this.id.setAccountRole(accountRole);
}

}