Hibernate:通过附加表进行多对多

发布于 2024-12-17 02:52:55 字数 1846 浏览 0 评论 0原文

我有这些表,组织可以是许多消息的发送者或接收者,一条消息可以有 2 个发送或接收的组织。我知道我的数据库没有意义,但我无法更改它:

在此处输入 imad 描述

那么,我是否将 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:

enter imad description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-12-24 02:52:56

我建议:

在我看来,这是 2-N 关系。一个组织可以发送许多消息,而一条消息只能由一个或两个组织发送或接收。您不需要连接表。

将一组消息放入组织中 - 这将代表组织发送/接收的消息。您还可以制作 2 套 - sentMSGSreceivedMSGS

并将组织的 FK 放入消息本身中,因为这将代表消息的组织。您可以在消息中添加 sentIDreceviedID

我想这会让表格和程序的理解变得更容易。

如果您选择保留原始关系,多对多关系将表示为:

1-N-1 (Organization-SenderReceiver-Msg)。

Organization-SenderReceiver 为 1-N
SenderReceiver-Msg 为 N-1。

因此,您将 SenderReceiver 集合放入 OrganizationMsg 类中。以及 SenderReceiver 内的 OrganizationMsg 对象。

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 and receivedMSGS.

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 and receviedID 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-N
and SenderReceiver-Msg is N-1.

So you put a collection of SenderReceiver in both the Organization and the Msg classes. And objects of both Organization and Msg inside the SenderReceiver.

浪漫之都 2024-12-24 02:52:56

SenderReceiver 表具有 Organization 的外键和 Message 的外键。这是一个明显的迹象,表明发送者接收者只能有一个消息和一个组织。

因此,您

  • 在 Organization 和 SenderReceiver 之间有一个 OneToMany、
  • 在 SenderReceiver 和 Organization 之间有一个 ManyToOne、
  • 在 Message 和 SenderReceiver 之间有一个 OneToMany、
  • 在 SenderReceiver 和 Message 之间有一个 ManyToOne

是否映射所有这些关联取决于您。每个关联可以是单向的或双向的。

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

  • a OneToMany between Organization and SenderReceiver,
  • a ManyToOne between SenderReceiver and Organization,
  • a OneToMany between Message and SenderReceiver
  • a ManyToOne between SenderReceiver and Message

Whether you map all those associations is up to you. Each association may be unidirectional or bidirectional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文