Hibernate:通过附加表进行多对多
我有这些表,组织可以是许多消息的发送者或接收者,一条消息可以有 2 个发送或接收的组织。我知道我的数据库没有意义,但我无法更改它:
那么,我是否将 SenderReceiver 连接到 Msg作为 1 对 n,组织到 SenderReceiver 作为 n 对 1?或者反之亦然 n-to1 和 1-to-n?
发送方接收方:
public class Senderreceiver implements java.io.Serializable {
private Set<Organization> organizations = new HashSet();
private Set<Msg> msg = new HashSet();
xml 文件:
<set fetch="select" inverse="true" lazy="true" name="msgs" table="MSG">
<key>
<column name="MsgID" not-null="true"/>
</key>
<one-to-many class="entity3.Msg"/>
</set>
<set fetch="select" inverse="true" lazy="true" name="organizations" table="ORGANIZATION">
<key>
<column name="OrganizationID" not-null="true"/>
</key>
<one-to-many class="entity3.Organization"/>
</set>
消息:
public class Msg implements java.io.Serializable {
private Senderreceiver senderreceiver;
xml:
<many-to-one class="entity3.Senderreceiver" fetch="select" name="senderreceiver">
<column name="SenderReceiverID" not-null="true"/>
</many-to-one>
组织:
public class Organization implements java.io.Serializable {
private Senderreceiver senderreceiver;
xml :
<many-to-one class="entity3.Senderreceiver" fetch="select" name="senderreceiver">
<column name="SenderReceiverID" not-null="true"/>
</many-to-one>
任何帮助将不胜感激。提前致谢!
I have these tables, organization can be a sender or receiver of many messages, a msg can have 2 organizations that send it or receive. I know that my db doesnt make sense but i cant change it:
So, do i connect SenderReceiver to Msg as 1-to-n, and Organization to SenderReceiver as n-to-1? or vice versa n-to1 and 1-to-n?
Senderreceiver:
public class Senderreceiver implements java.io.Serializable {
private Set<Organization> organizations = new HashSet();
private Set<Msg> msg = new HashSet();
xml file:
<set fetch="select" inverse="true" lazy="true" name="msgs" table="MSG">
<key>
<column name="MsgID" not-null="true"/>
</key>
<one-to-many class="entity3.Msg"/>
</set>
<set fetch="select" inverse="true" lazy="true" name="organizations" table="ORGANIZATION">
<key>
<column name="OrganizationID" not-null="true"/>
</key>
<one-to-many class="entity3.Organization"/>
</set>
Msg:
public class Msg implements java.io.Serializable {
private Senderreceiver senderreceiver;
xml:
<many-to-one class="entity3.Senderreceiver" fetch="select" name="senderreceiver">
<column name="SenderReceiverID" not-null="true"/>
</many-to-one>
Organization:
public class Organization implements java.io.Serializable {
private Senderreceiver senderreceiver;
xml:
<many-to-one class="entity3.Senderreceiver" fetch="select" name="senderreceiver">
<column name="SenderReceiverID" not-null="true"/>
</many-to-one>
Any help would be appreceited. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议:
在我看来,这是 2-N 关系。一个组织可以发送许多消息,而一条消息只能由一个或两个组织发送或接收。您不需要连接表。
将一组消息放入组织中 - 这将代表组织发送/接收的消息。您还可以制作 2 套 -
sentMSGS
和receivedMSGS
。并将组织的 FK 放入消息本身中,因为这将代表消息的组织。您可以在消息中添加
sentID
和receviedID
。我想这会让表格和程序的理解变得更容易。
如果您选择保留原始关系,多对多关系将表示为:
1-N-1 (
Organization-SenderReceiver-Msg
)。Organization-SenderReceiver
为 1-NSenderReceiver-Msg
为 N-1。因此,您将
SenderReceiver
集合放入Organization
和Msg
类中。以及SenderReceiver
内的Organization
和Msg
对象。I would suggest this:
As I see it, this is 2-N relation. An organization can send many messages and a message can be sent or received only by one or two organization. You dont need a connection table.
put a set of messages in the organization - this will represent the messages sent/received by the organization. You can also make 2 sets -
sentMSGS
andreceivedMSGS
.and put a FK of the organization in the message itself, as this will represnt the oganization of the message. You can put a
sentID
andreceviedID
in a message.I think this will make the understanding of the table and the program more easy.
If you choose to stay with the original relations, a many to many relation will be presented as:
1-N-1 (
Organization-SenderReceiver-Msg
).Organization-SenderReceiver
is 1-Nand
SenderReceiver-Msg
is N-1.So you put a collection of
SenderReceiver
in both theOrganization
and theMsg
classes. And objects of bothOrganization
andMsg
inside theSenderReceiver
.SenderReceiver 表具有 Organization 的外键和 Message 的外键。这是一个明显的迹象,表明发送者接收者只能有一个消息和一个组织。
因此,您
是否映射所有这些关联取决于您。每个关联可以是单向的或双向的。
The SenderReceiver table has a foreign key to Organization, and a foreign key to Message. This is a clear sign that a SenderReceiver may only have one Message, and One organization.
You thus have
Whether you map all those associations is up to you. Each association may be unidirectional or bidirectional.