我想创建实体用户组来链接一些用户id和组id。我想我想我想得太多了,现在我找不到解决方案。
@Entity
@Table(name = "users")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private String userId;
private String name;
@OneToMany(mappedBy = "user")
private List<UserGroups> userGroups;
@Table(name = "groups")
@Data
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "group_id")
private String groupId;
private String category;
private String name;
private String description;
@OneToMany(mappedBy = "group")
private List<UserGroups> userGroups;
@Entity
@Data
@Table(name = "user_groups")
public class UserGroups {
@EmbeddedId
UserGroupsCompositeKey id;
@ManyToOne
@MapsId("userId")
@JoinColumn(name = "user_id")
private Users user;
@ManyToOne
@MapsId("featureId")
@JoinColumn(name = "group_id")
private Group group;
@Embeddable
public class UserGroupsCompositeKey implements Serializable {
@Column(name = "user_id")
String userId;
@Column(name = "group_id")
String groupId;
}
我想发送post请求,比如“/group/{group\u id}/users”,以便在请求体中发送一些用户id列表来连接它们。
我想我用复合密钥过度配置了这个解决方案。有没有更简单易读的解决方案?
编辑:此解决方案没有按我希望的那样工作。我用compositekey创建了jparepository类(不确定是否正确)
@Repository
public interface ProductFeaturesRepository extends JpaRepository<UserGroups, UserGroupsCompositeKey> {
}
我想在group类中添加一些controller方法,将一些用户添加到具有请求主体的group中,如:
Endpoint:/group/{group_id}/users
Body:
[
{
"userId": "USER-NR423423534634"
},
{
"userId": "USER-NR2355321"
}
]
它工作不正常,请求之后没有任何内容添加到数据库中
暂无答案!
目前还没有任何答案,快来回答吧!