JPA中的OneToOne和ManyToOne关系,涉及相同实体。

huangapple go评论63阅读模式
英文:

JPA OneToOne and ManyToOne relationship with same entity

问题

我在Java世界和JPA中还是新手。我在OneToMany关系方面遇到了问题。

我有两个实体:UserEntityManagerEntity

Manager是一个被提升的User,所以在这里我们有一个OneToOne关系,managers表中的manager_uuid引用了users表中的uuid。这个关系运行良好。

现在每个Manager有许多clients,即UserEntity,它由managers表进行映射。

问题是它一直在寻找users表中的client_uuid,而不是managers,我不知道为什么...

@Entity
@Table(name = "users")
public class UserEntity {

  @Id
  @GeneratedValue(strategy = AUTO)
  private UUID uuid;

  @OneToOne
  @JoinColumn(
      name = "manager_uuid",
      referencedColumnName = "uuid")
  private ManagerEntity managerReference;

  @ManyToOne
  @JoinColumn(name = "client_uuid",
      referencedColumnName = "uuid")
  private ManagerEntity manager;
}

@Entity
@Table(name = "managers")
public class ManagerEntity {

  @Id
  @GeneratedValue(strategy = AUTO)
  private UUID uuid;

  @OneToOne(mappedBy = "managerReference")
  private UserEntity actingManager;

  @OneToMany(mappedBy = "manager")
  private List<UserEntity> clients = new ArrayList<>();
}

迁移:

CREATE TABLE IF NOT EXISTS managers
(
    uuid            uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    manager_uuid    uuid NOT NULL,
    client_uuid     uuid NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    FOREIGN KEY (manager_uuid) REFERENCES users (uuid),
    FOREIGN KEY (client_uuid) REFERENCES users (uuid)
)

错误信息:

> 错误:列userentity0_.client_uuid不存在

我做错了什么,有任何想法吗?

英文:

I'm new in Java world, and JPA. I have a problem with OneToMany relationships.

So I have two entities: UserEntity and ManagerEntity.

The Manager is a User that was promoted, so here we have OneToOne relationship where manager_uuid in managers table reference uuid in the users table. This relationship works fine.

Now each Manager has many clients => UserEntity and it is mapped by managers table.

The problem is that it keeps looking for client_uuid in the users table instead of managers and I don't know why...

@Entity
@Table(name = &quot;users&quot;)
public class UserEntity {

  @Id
  @GeneratedValue(strategy = AUTO)
  private UUID uuid;

  @OneToOne
  @JoinColumn(
      name = &quot;manager_uuid&quot;,
      referencedColumnName = &quot;uuid&quot;)
  private ManagerEntity managerReference;

  @ManyToOne
  @JoinColumn(name = &quot;client_uuid&quot;,
      referencedColumnName = &quot;uuid&quot;)
  private ManagerEntity manager;
}

@Entity
@Table(name = &quot;managers&quot;)
public class ManagerEntity {

  @Id
  @GeneratedValue(strategy = AUTO)
  private UUID uuid;

  @OneToOne(mappedBy = &quot;managerReference&quot;)
  private UserEntity actingМanager;

  @OneToMany(mappedBy = &quot;manager&quot;)
  private List&lt;UserEntity&gt; clients = new ArrayList&lt;&gt;();
}

Migration:

CREATE TABLE IF NOT EXISTS managers
(
    uuid            uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    manager_uuid    uuid NOT NULL,
    client_uuid     uuid NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    FOREIGN KEY (manager_uuid) REFERENCES users (uuid),
    FOREIGN KEY (client_uuid) REFERENCES users (uuid)
)

The error:

> ERROR: column userentity0_.client_uuid does not exist

Any idea what I'm doing wrong?

答案1

得分: 1

这是预期的工作方式。client_uuid 是需要存在于用户表中的内容。由于一个经理可能有多个客户,所以不能将多个 client_uuid 插入到经理表的单个字段中。但是当情况反过来时,一个用户只会有一个经理。因此,他们可以轻松存储 manager_uuid。

您可以尝试将代码编写如下:

CREATE TABLE IF NOT EXISTS managers
(
    uuid            uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    manager_uuid    uuid NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    FOREIGN KEY (manager_uuid) REFERENCES users (uuid)
)

CREATE TABLE IF NOT EXISTS users 
(
    uuid            uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    client_uuid     uuid NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    FOREIGN KEY (client_uuid) REFERENCES managers(uuid)
)
英文:

It's working as expected. The client_uuid is something which needs to be in user table. As one manager will be having multiple clients, you can't insert multiple client_uuid in manager table single field. But when it comes the other way around than one user will be having only one manager. So they can store the manager_uuid easily.

You can try make your code like below:

CREATE TABLE IF NOT EXISTS managers
(
    uuid            uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    manager_uuid    uuid NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    FOREIGN KEY (manager_uuid) REFERENCES users (uuid)
)

CREATE TABLE IF NOT EXISTS users 
(
    uuid            uuid DEFAULT gen_random_uuid() PRIMARY KEY,
    client_uuid     uuid NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    FOREIGN KEY (client_uuid) REFERENCES managers(uuid)
)

huangapple
  • 本文由 发表于 2020年8月25日 11:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63571505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定