错误:插入新数据@manytomany时“表中不存在键”

w1jd8yoj  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(409)

我是新手,所以我希望我能正确地解释这个。我有一个用户注册/登录和用户角色的项目。现在我想创建一个todo函数,用户可以在其中创建自己的任务。我想在“任务”和“用户”之间建立一个@manytomy关系。
当我创建一个新任务时,一切都正常,直到我想在数据库的“tasks\u users”中创建一个新行,这里我得到了错误: Key (task_id)=(35) is not present in table "users". 这是我的用户模型:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

private String email;
private String password;

@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST})
@JoinTable(
        name = "users_roles",
        joinColumns = @JoinColumn(
                name = "user_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                name = "role_id", referencedColumnName = "id"))

private Collection<Role> roles;

// constructors:
public User(){
    // default constructor
}

public User(String firstName, String lastName, String email, String password, Collection<Role> roles) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.password = password;
    this.roles = roles;
}

// getters and setters:

以及任务的模型:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "name")
private String taskName;

@Column(name = "description")
private String taskDesc;

@Column(name = "target_date")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
private Date targetDate;

@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.ALL})
@JoinTable(
        name = "tasks_users",
        joinColumns = @JoinColumn(
                name = "task_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                name = "user_id", referencedColumnName = "id"))

private Collection<User> users;

public Task() {
    super();
}

public Task(String taskName, String taskDesc, Date targetDate, Collection<User> users) {
    super();
    this.taskName = taskName;
    this.taskDesc = taskDesc;
    this.targetDate = targetDate;
    this.users = users;
}

当我添加一个新任务时,这是以下错误代码:

`select user0_.id as id1_3_, user0_.email as email2_3_, user0_.first_name as first_na3_3_, 
user0_.last_name as last_nam4_3_, user0_.password as password5_3_ from users user0_ where user0_.email=
binding parameter [1] as [VARCHAR] - [email]
extracted value ([id1_3_] : [BIGINT]) - [2]
extracted value ([email2_3_] : [VARCHAR]) - [email]
extracted value ([first_na3_3_] : [VARCHAR]) - [name]
extracted value ([last_nam4_3_] : [VARCHAR]) - [name]
extracted value ([password5_3_] : [VARCHAR]) - [$2a$10$0JQutx1DVWaPhiJGGhdQlO8pm7BtOviSh6srTmKrmIk4WOnL9VGb6]`

Created collection wrapper: [nl.eind.java.backend.model.User.roles#2]

select roles0_.user_id as user_id1_4_0_, roles0_.role_id as role_id2_4_0_, role1_.id as id1_0_1_, 
role1_.name as name2_0_1_ from users_roles roles0_ inner join role role1_ on 
roles0_.role_id=role1_.id 
where roles0_.user_id=?

binding parameter [1] as [BIGINT] - [2]
extracted value ([id1_0_1_] : [BIGINT]) - [1]
extracted value ([name2_0_1_] : [VARCHAR]) - [ROLE_ADMIN]
extracted value ([user_id1_4_0_] : [BIGINT]) - [2]
extracted value ([role_id2_4_0_] : [BIGINT]) - [1]

insert into tasks (target_date, description, name) values (?, ?, ?)
binding parameter [1] as [TIMESTAMP] - [Tue Apr 06 00:00:00 CEST 2021]
binding parameter [2] as [VARCHAR] - [d]
binding parameter [3] as [VARCHAR] - [d]

insert into tasks_users (task_id, user_id) values (?, ?)
binding parameter [1] as [BIGINT] - [35]
binding parameter [2] as [BIGINT] - [2]

SQL Error: 0, SQLState: 23503
ERROR: insert or update on table "tasks_users" violates foreign key constraint "fk4jyg0wr5a9mher3c5ys65wefu"
Detail: Key (task_id)=(35) is not present in table "users".
HHH000010: On release of batch it still contained JDBC statements

org.postgresql.util.PSQLException: ERROR: insert or update on table "tasks_users" violates foreign key 
constraint "fk4jyg0wr5a9mher3c5ys65wefu"
Detail: Key (task_id)=(35) is not present in table "users".
fnx2tebb

fnx2tebb1#

@JoinTable(
        name = "tasks_users",
        joinColumns = @JoinColumn(
                name = "task_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(
                name = "user_id", referencedColumnName = "id"))

该部分应仅在这2个实体中的1个实体上。用户或任务。将拥有该部分代码的实体( @JoinTable )是关系的主人。
正如这里所记录的JavaEE规范
指定关联的Map。它应用于关联的拥有方。

相关问题