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

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

本文介绍了如何使用Annotations在Hibernate中表示组合键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从我的数据库中反向设计了一些表格,当我尝试将我的对象保存到数据库时,出现以下错误:

初始SessionFactory创建失败.hibernate.AnnotationException:引用com.mycode.Account中的com.mycode.Block的外键具有错误的列号。应该是2
线程main中的异常java.lang.ExceptionInInitializerError



域对象是包含多个Account对象的Block:

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

账户具有Id和Role的组合键。这已经在一个单独的类中设置:

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

私人长块blockOid;
私人字符串accountRole;
$ b $ public BlockAccountId(){
}

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

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

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

$ b @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作为组合键。



任何示例都将不胜感激!



注意:这是一个帐户(多个)关系。

谢谢 解决方案

直接在嵌入式ID组件中。

示例(仅编写重要的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
公共类帐户{

@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(可选= false)
私有块块;

@Column(name =ACCOUNT_ROLE,nullable = false,length = 10)
private String accountRole;
$ b $ public BlockAccountId(){

}

//实现equals和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`)参考`block`(`BLOCK_OID`)


So I reverse engineered some tables from my db and when I try to save my object to the db I get the following error:

Initial SessionFactory creation failed.org.hibernate.AnnotationException: A Foreign key refering com.mycode.Block from com.mycode.Account has the wrong number of column. should be 2Exception in thread "main" java.lang.ExceptionInInitializerError

The Domain objects Are Block which contains a number of Account Objects:

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

Account has a Composite key of Id and Role. This has been setup in a seperate Class:

@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;
}

So I want to know. How can I Link the tables Block and account on blockOid but still ensure the account table has both blockOid and accountRole as a composite key.

Any examples would be greatly appreciated!

N.B this is a Block (One) to Account (Many) relationship.

Thanks

解决方案

The easiest is to place your association directly in the embedded id component.

Example (Only write the important getter() and 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
}

The corresponding database table are :

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`)
)

这篇关于如何使用Annotations在Hibernate中表示组合键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:43