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

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

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,我不知道为什么...

  1. @Entity
  2. @Table(name = "users")
  3. public class UserEntity {
  4. @Id
  5. @GeneratedValue(strategy = AUTO)
  6. private UUID uuid;
  7. @OneToOne
  8. @JoinColumn(
  9. name = "manager_uuid",
  10. referencedColumnName = "uuid")
  11. private ManagerEntity managerReference;
  12. @ManyToOne
  13. @JoinColumn(name = "client_uuid",
  14. referencedColumnName = "uuid")
  15. private ManagerEntity manager;
  16. }
  17. @Entity
  18. @Table(name = "managers")
  19. public class ManagerEntity {
  20. @Id
  21. @GeneratedValue(strategy = AUTO)
  22. private UUID uuid;
  23. @OneToOne(mappedBy = "managerReference")
  24. private UserEntity actingManager;
  25. @OneToMany(mappedBy = "manager")
  26. private List<UserEntity> clients = new ArrayList<>();
  27. }

迁移:

  1. CREATE TABLE IF NOT EXISTS managers
  2. (
  3. uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  4. manager_uuid uuid NOT NULL,
  5. client_uuid uuid NOT NULL,
  6. created_at TIMESTAMP NOT NULL,
  7. updated_at TIMESTAMP NOT NULL,
  8. FOREIGN KEY (manager_uuid) REFERENCES users (uuid),
  9. FOREIGN KEY (client_uuid) REFERENCES users (uuid)
  10. )

错误信息:

> 错误:列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...

  1. @Entity
  2. @Table(name = &quot;users&quot;)
  3. public class UserEntity {
  4. @Id
  5. @GeneratedValue(strategy = AUTO)
  6. private UUID uuid;
  7. @OneToOne
  8. @JoinColumn(
  9. name = &quot;manager_uuid&quot;,
  10. referencedColumnName = &quot;uuid&quot;)
  11. private ManagerEntity managerReference;
  12. @ManyToOne
  13. @JoinColumn(name = &quot;client_uuid&quot;,
  14. referencedColumnName = &quot;uuid&quot;)
  15. private ManagerEntity manager;
  16. }
  17. @Entity
  18. @Table(name = &quot;managers&quot;)
  19. public class ManagerEntity {
  20. @Id
  21. @GeneratedValue(strategy = AUTO)
  22. private UUID uuid;
  23. @OneToOne(mappedBy = &quot;managerReference&quot;)
  24. private UserEntity actingМanager;
  25. @OneToMany(mappedBy = &quot;manager&quot;)
  26. private List&lt;UserEntity&gt; clients = new ArrayList&lt;&gt;();
  27. }

Migration:

  1. CREATE TABLE IF NOT EXISTS managers
  2. (
  3. uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  4. manager_uuid uuid NOT NULL,
  5. client_uuid uuid NOT NULL,
  6. created_at TIMESTAMP NOT NULL,
  7. updated_at TIMESTAMP NOT NULL,
  8. FOREIGN KEY (manager_uuid) REFERENCES users (uuid),
  9. FOREIGN KEY (client_uuid) REFERENCES users (uuid)
  10. )

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。

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

  1. CREATE TABLE IF NOT EXISTS managers
  2. (
  3. uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  4. manager_uuid uuid NOT NULL,
  5. created_at TIMESTAMP NOT NULL,
  6. updated_at TIMESTAMP NOT NULL,
  7. FOREIGN KEY (manager_uuid) REFERENCES users (uuid)
  8. )
  9. CREATE TABLE IF NOT EXISTS users
  10. (
  11. uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  12. client_uuid uuid NOT NULL,
  13. created_at TIMESTAMP NOT NULL,
  14. updated_at TIMESTAMP NOT NULL,
  15. FOREIGN KEY (client_uuid) REFERENCES managers(uuid)
  16. )
英文:

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:

  1. CREATE TABLE IF NOT EXISTS managers
  2. (
  3. uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  4. manager_uuid uuid NOT NULL,
  5. created_at TIMESTAMP NOT NULL,
  6. updated_at TIMESTAMP NOT NULL,
  7. FOREIGN KEY (manager_uuid) REFERENCES users (uuid)
  8. )
  9. CREATE TABLE IF NOT EXISTS users
  10. (
  11. uuid uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  12. client_uuid uuid NOT NULL,
  13. created_at TIMESTAMP NOT NULL,
  14. updated_at TIMESTAMP NOT NULL,
  15. FOREIGN KEY (client_uuid) REFERENCES managers(uuid)
  16. )

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:

确定