我在REST控制器(acceptUseroffermapping
)中创建方法(UserOfferController
),我想在其中删除数据库(UserOfferMapping
表)中的记录。但是问题是我运行此方法后,记录没有删除,关系也保存了。
我也有UserOfferMapping
类,它映射到User
类。在UserOfferController
中,我使用UserOfferMapping
进行操作:创建,从数据库中选择记录,并尝试删除记录但失败。
UserOfferController.java:
/*...*/
@POST
@RequestMapping("/acceptUserOfferMapping")
public void acceptUseroffermapping(@RequestBody Map<String,
String> body) throws ParseException {
String userId = body.get("userId");
String offerId = body.get("offerId");
Optional<User> user = userRepository.findById(Integer.parseInt(userId));
UserOfferMapping mapping = userOfferMappingRepository.getById(Integer.parseInt(userId));
user.get().getUserOfferMapping().remove(mapping);
userRepository.save(user.get());
userOfferMappingRepository.deleteById(Integer.parseInt(offerId));
}
/*...*/
User.java:
/*some imports*/
@Entity
@Table(name = "User")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
/* ...
* a lot of fields
* ...
*/
// Important section which describes all Role Project and Skill mapping
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<UserUserrolemapping> userrolemapings = new HashSet<>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<Userprojectmapping> userprojectmappings = new HashSet<>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<UserOfferMapping> userOfferMapping = new HashSet<>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@OrderBy
private Set<Userskillmapping> userskillmappings = new HashSet<>();
/* ...
* a lot of fields too
* ...
*/
/* getter and setters */
}
UserOfferMappingRepository.java:
public interface UserOfferMappingRepository extends JpaRepository<UserOfferMapping, Integer> {
public List<UserOfferMapping> getAllByUser(Optional<User> user);
public UserOfferMapping getUserOfferMappingByUserAndProjectAndUserRole(User user, Userproject userproject, Userrole userrole);
public UserOfferMapping getById(int id);
public void deleteById(int id);
}
UserOfferMapping.java:
@Entity
public class UserOfferMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "userid")
@JsonBackReference
private User user;
@ManyToOne
@JoinColumn(name = "roleid")
private Userrole userRole;
@ManyToOne
@JoinColumn(name = "projectid")
private Userproject project;
@Column(name = "fromdate", nullable = true)
private Date fromdate;
@Column(name = "todate", nullable = true)
private Date todate;
@Column(name = "chance")
private int chance;
@Column(name = "percent")
private int percent;
public int getId() {
return id;
}
public User getUser() {
return user;
}
public Userrole getUserRole() {
return userRole;
}
public Userproject getProject() {
return project;
}
public Date getFromdate() {
return fromdate;
}
public int getChance() {
return chance;
}
public int getPercent() {
return percent;
}
public void setId(int id) {
this.id = id;
}
public void setUser(User user) {
this.user = user;
}
public void setUserRole(Userrole userRole) {
this.userRole = userRole;
}
public void setProject(Userproject project) {
this.project = project;
}
public void setFromdate(Date fromdate) {
this.fromdate = fromdate;
}
public void setChance(int chance) {
this.chance = chance;
}
public void setPercent(int percent) {
this.percent = percent;
}
public void setTodate(Date todate) {
this.todate = todate;
}
public Date getTodate() {
return todate;
}
}
答案 0 :(得分:1)
您可以尝试使用此
public interface UserOfferMappingRepository extends JpaRepository<UserOfferMapping, Integer> {
public List<UserOfferMapping> getAllByUser(Optional<User> user);
public UserOfferMapping getUserOfferMappingByUserAndProjectAndUserRole(User user, Userproject userproject, Userrole userrole);
public UserOfferMapping getById(int id);
// public void deleteById(int id);
@Modifying(clearAutomatically = true)
@Query(value = "Delete from UserOfferMapping c WHERE c.id=:id")
public void deleteById(@Param("id") int id);
}
答案 1 :(得分:0)
因此,您具有双向实体关联。
尝试在mapping.setUser(null);
之前添加userRepository.save
。
答案 2 :(得分:0)
持久化和删除对象需要 JPA 中的事务,这就是为什么你必须在 Repository 中的方法之前定义 @Transactional 注释,例如`
@Transactional
public void deleteById(@Param("id") int id);