在休眠中使用共享外键列映射两个实体
我有四个实体要映射在一起:“关联”、“帐户”、“交易”和“交易事件”。 Association的id是一个简单的整数id。帐户和交易均嵌入了由关联映射和数字组成的 ID。
TransactionEvent 应该有一个嵌入的 id,由一个 Account 和一个 Association 组成。现在,其中每一个都映射到一个关联,我希望它与一个 TransactionEvent 是相同的关联。
JPA 注释用于 Hibernate 映射,但我无法使其工作。我尝试强制关联键使用相同的列名称,但 Hibernate 抱怨重复的列。
这个问题可以解决吗,还是我思路不清晰?
以下是带注释的类,但我删除了 getter/setter 和非 id 列以及来自 javax.persistence 命名空间的注释:
@Entity
public class Association implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
}
@Embeddable
public class AccountPK implements Serializable {
@ManyToOne(optional=false)
private Association association;
@Column(nullable=false)
private int number;
}
@Embeddable
public class TransactionPK implements Serializable {
@ManyToOne
private Association association;
@GeneratedValue(strategy=GenerationType.AUTO)
private long number;
}
@Embeddable
public class AccountEventPK implements Serializable {
@ManyToOne(optional=false)
@JoinColumns({
@JoinColumn(name="association_id", referencedColumnName="association_id"),
@JoinColumn(name="account_number", referencedColumnName="number")
})
private Account account;
@ManyToOne(optional=false)
@JoinColumns({
@JoinColumn(name="association_id", referencedColumnName="association_id"),
@JoinColumn(name="transaction_number", referencedColumnName="number")
})
private Transaction transaction;
}
实际帐户、事务和 AccountEvent 实体位于表单上
@Entity
public class Account implements Serializable {
@EmbeddedId
private AccountPK id;
}
I have four entities to map together, "Association", "Account", "Transaction" and "TransactionEvent". The id of Association is a simple integer id. Account and Transaction each have embedded id's consisting of a mapping to an Association and a number.
TransactionEvent should have an embedded id consisting of one Account and one Association. Now, each of those are mapped to an Association, and I want it to be the same Association for one TransactionEvent.
JPA Annotations is used for the Hibernate mapping, but I cannot make this work. I have tried forcing the same column name for the Association key, but Hibernate complains about repeated columns.
Is this possible to solve, or am I not thinking straight?
Here are the annotated classes, but I trimmed away getters/setters and non-id columns, annotations from the javax.persistence namespace:
@Entity
public class Association implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
}
@Embeddable
public class AccountPK implements Serializable {
@ManyToOne(optional=false)
private Association association;
@Column(nullable=false)
private int number;
}
@Embeddable
public class TransactionPK implements Serializable {
@ManyToOne
private Association association;
@GeneratedValue(strategy=GenerationType.AUTO)
private long number;
}
@Embeddable
public class AccountEventPK implements Serializable {
@ManyToOne(optional=false)
@JoinColumns({
@JoinColumn(name="association_id", referencedColumnName="association_id"),
@JoinColumn(name="account_number", referencedColumnName="number")
})
private Account account;
@ManyToOne(optional=false)
@JoinColumns({
@JoinColumn(name="association_id", referencedColumnName="association_id"),
@JoinColumn(name="transaction_number", referencedColumnName="number")
})
private Transaction transaction;
}
Actual Account, Transaction and AccountEvent entities are on the form
@Entity
public class Account implements Serializable {
@EmbeddedId
private AccountPK id;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对直接将关联放置在嵌入式 id 组件中没有太多经验,因为 JPA 不支持这种方式,但这是 Hibernate 特有的。
作为替代方案,我的建议是使用复合主键 JPA wikibook 的部分:
这看起来就是您正在寻找的内容(并且可能不那么复杂)。我会尝试(增量)实施这个解决方案。
I don't have much experience with placing associations directly in the embedded id component since this is not supported by JPA but is Hibernate specific.
As an alternative my suggestion would be to use the approach described in the Composite Primary Keys section of the JPA wikibook:
This looks like to be what you're looking for (and maybe less complicated). I'd try to implement this solution (incrementally).