英文:
JPA OneToOne and ManyToOne relationship with same entity
问题
我在Java世界和JPA中还是新手。我在OneToMany关系方面遇到了问题。
我有两个实体:UserEntity和ManagerEntity。
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 = "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 actingМanager;
@OneToMany(mappedBy = "manager")
private List<UserEntity> clients = new ArrayList<>();
}
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)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论