@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
}
$ c $
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中表示组合键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!