gpt4 book ai didi

spring-boot - Spring Data 休息如何在@manytomany 关系上执行 CRUD,具有额外列的复合表

转载 作者:行者123 更新时间:2023-12-02 03:05:18 25 4
gpt4 key购买 nike

我无法通过来自 Restful 客户端 Postman 的 json POST 在具有额外列的复合表上执行 CRUD。我正在使用 Spring boot、spring data rest 和 spring JPA。
我在数据库中有 3 个表
-用户
-能力
-user_competency(带有额外列的连接/复合表)

enter image description here
这是我的课

用户

@Entity
@Table(name = "\"user\"", schema = "public")
@JsonIdentityInfo(
generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "userId")
public class User implements java.io.Serializable {

private Long userId;

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "user_id", unique = true, nullable = false)
public Long getUserId() {
return this.userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0);

@OneToMany(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, mappedBy = "user")
public Set<UserCompetency> getUserCompetencies() {
return this.userCompetencies;
}

public void setUserCompetencies(Set<UserCompetency> userCompetencies) {
this.userCompetencies = userCompetencies;
}

}

能力
@Entity
@Table(name = "competency", schema = "public")
@JsonIdentityInfo(
generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "competencyId")
public class Competency implements java.io.Serializable {


private Long competencyId;
private Set<UserCompetency> userCompetencies = new HashSet<UserCompetency>(0);

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "competency_id", unique = true, nullable = false)
public Long getCompetencyId() {
return this.competencyId;
}

public void setCompetencyId(Long competencyId) {
this.competencyId = competencyId;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "competency")
public Set<UserCompetency> getUserCompetencies() {
return this.userCompetencies;
}

public void setUserCompetencies(Set<UserCompetency> userCompetencies) {
this.userCompetencies = userCompetencies;
}
}

用户能力
@Entity
@Table(name = "user_competency", schema = "public")
@JsonIdentityInfo(
generator =ObjectIdGenerators.IntSequenceGenerator.class,
property = "id")
public class UserCompetency implements java.io.Serializable {
private UserCompetencyId id;
private Level level;
private User user;
private Competency competency;

@EmbeddedId

@AttributeOverrides({
@AttributeOverride(name = "competencyId", column = @Column(name = "competency_id", nullable = false)),
@AttributeOverride(name = "userId", column = @Column(name = "user_id", nullable = false)) })
public UserCompetencyId getId() {
return this.id;
}

public void setId(UserCompetencyId id) {
this.id = id;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "level_id")
public Level getLevel() {
return this.level;
}

public void setLevel(Level level) {
this.level = level;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
public User getUser() {
return this.user;
}

public void setUser(User user) {
this.user = user;
}

@ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
public Competency getCompetency() {
return this.competency;
}

public void setCompetency(Competency competency) {
this.competency = competency;
}
}

用户能力 ID
@Embeddable
public class UserCompetencyId implements java.io.Serializable {

private Long competencyId;
private Long userId;

public UserCompetencyId() {
}

public UserCompetencyId(Long competencyId, Long userId) {
this.competencyId = competencyId;
this.userId = userId;
}


@Column(name = "competency_id", nullable = false)
public Long getCompetencyId() {
return this.competencyId;
}

public void setCompetencyId(Long competencyId) {
this.competencyId = competencyId;
}

@Column(name = "user_id", nullable = false)
public Long getUserId() {
return this.userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof UserCompetencyId))
return false;
UserCompetencyId castOther = (UserCompetencyId) other;

return (this.getCompetencyId() == castOther.getCompetencyId()) && (this.getUserId() == castOther.getUserId());
}
}

假设我已经在 User 和 Competency 表中进行了记录,并且我想将我尝试像这样发布的两者关联起来,但是它给了我 405 Method Not Allowed 的错误。

enter image description here
需要帮助,要发布的 json 结构应该是什么 用户已经存在并且能力可能存在,或者可以添加新用户并与现有用户相关联。

最佳答案

使用此代码,我可以发布新关系:

UserCompetency.class

@Entity
@Table(name = "user_competency")
@IdClass(UserCompetencyId.class)
public class UserCompetency implements java.io.Serializable {

@Id @ManyToOne
@JoinColumn(name = "competency_id", nullable = false, insertable = false, updatable = false)
private Competency competency;

@Id @ManyToOne
@JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
private User user;

UserCompetencyId.class
public class UserCompetencyId implements java.io.Serializable {

private Long competency;

private Long user;

public UserCompetencyId() {
}

public UserCompetencyId(Long competency, Long user) {
this.competency = competency;
this.user = user;
}

UserCompetencyRepository.class
public interface UserCompetencyRepository extends JpaRepository<UserCompetency, UserCompetencyId> {

}

发布 http://localhost:8080/userCompetencies
{
"competency": "/competencies/2"
, "user": "/user/4"
}

关于spring-boot - Spring Data 休息如何在@manytomany 关系上执行 CRUD,具有额外列的复合表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43186824/

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