gpt4 book ai didi

java - Spring Data JPA - 删除子实体而不是在更新时设置为空?

转载 作者:行者123 更新时间:2023-11-30 22:26:07 24 4
gpt4 key购买 nike

我有以下领域模型

users
----
id (PK)
name

orders
------
id (PK)
userid (PK)
name

Orders.userid 引用了User 中的id,是复合主键的一部分。 (我需要在主键中使用 userid,因为它是一个分区键,而在 MySQL 中,我们不能在没有分区键的情况下创建主键)

在使用 JPA 更新用户时,如果我尝试使用清除关联的订单集合

User user = userRepository.getOne(id);
user.getOrders().clear();
userRepository.save(user);

JPA 首先尝试将关联订单上的 userid 设置为 null,然后删除该行。这会失败,因为 userid 不可为空并且是主键的一部分。有没有办法告诉 JPA 它只需要删除订单行而不先将 userid 列设置为空?

更新

映射:我的映射是这样的:

@Entity
@Table(name = "users")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
public Long id;

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

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="userid", referencedColumnName = "id", insertable = true, updatable = true)
@Fetch(FetchMode.JOIN)
private Set<Order> orders;

}

@Entity
@Table(name = "orders")
@IdClass(Order.OrderPk.class)
public class Order implements Serializable {

@Id
@Column(name="id")
private Long id;

@Id
@Column(name="userid")
private Long userId;

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

public static class OrderPk implements Serializable {

private Long id;

private Long userId;
}
}

你能告诉我映射会有什么变化吗?

更新:

也尝试了以下映射:

@Entity
@Table(name = "users")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", nullable = false)
public Long id;

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

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "orderPk.user")
@Fetch(FetchMode.JOIN)
private Set<Order> orders;
}

@Entity
@Table(name = "orders")
public class Order implements Serializable {

@EmbeddedId
private OrderPk orderPk;

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

@Embeddable
public static class OrderPk implements Serializable {

@GeneratedValue
@Column(name="id", insertable = false, updatable = false, nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userid", referencedColumnName = "id", insertable = false, updatable = false, nullable = false)
private User user;
}
}

在插入时,它提示说“为:class Order 生成了 null id”(也尝试过使用 insertable=true 和 updatable=true)

最佳答案

您可以使用标识符保留连接,并确保删除不会尝试使用集合上的可更新和可插入注释将 null 写入集合 FK。

@Entity
@Table(name = "users")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", nullable = false)
public Long id;

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

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "userId", updatable = false, insertable = false)
private Set<Order> orders;
}

@Entity
@Table(name = "orders")
public class Order implements Serializable {

@Id
@Column(name="id")
private Long id;

@Column(name="userid")
private Long userId;

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

@JoinColumn(name = "userId", updatable = false, insertable = false)

part表示子表不会更新,但是可以删除

关于java - Spring Data JPA - 删除子实体而不是在更新时设置为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35143661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com