0

我对学说实体映射有疑问。有一个基本的用户实体,我想创建一个 Friend 实体,由用户表的两个外键组成,友谊的第一个所有者和另一个。我尝试以这种方式( yaml )执行此操作,实体以正确的方式生成,但是当我使用学说方案更新创建数据库时,结果完全不同。

用户实体:

..\..\Entity\User:
type:  entity
uniqueConstraints:
    usernameCanonical:
        columns: usernameCanonical
    emailCanonical:
        columns: emailCanonical
oneToMany:
    owners:
        targetEntity: Friend
        mappedBy: owner
    friends:
        targetEntity: Friend
        mappedBy: friend

和朋友实体:

..\..\Entity\Friend:
type: entity
table: friend
lifecycleCallbacks:
    prePersist: [setCreated, setModified]
    preUpdate: [setModified]
manyToOne:
    owner:
        targetEntity: User
        inversedBy: friends
        joinColumn:
            name: user
            referencedColumnName: id
            onDelete: cascade
    friend:
        targetEntity: User
        inversedBy: owners
        joinColumn:
            name: user
            referencedColumnName: id
            onDelete: cascade

这应该创建两个从朋友到用户的外来对象,而是创建一个像:

ALTER TABLE friend ADD CONSTRAINT FK_55EEAC618D93D649 FOREIGN KEY (user) REFERENCES users (id) ON DELETE CASCADEe
4

1 回答 1

2

常见的“朋友”关系是 ManyToMany SelfReferencing 原则关系,它在原则文档中用作示例

于 2013-05-31T10:11:42.137 回答