Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring - Creating new entity plus association not working

I am having two entities AppUser and UserGroup. They have a @ManyToMany relationship.

I am currently trying to create and associate a new group for an existing user like this:

POST http://localhost:8080/groups

{
  "name": "Group 1", 
  "users": ["http://localhost:8080/users/1"]
}

The problem is that only the group gets created - but no association with the listed users - and still the server responds with 201 Created?!

These are the two repositories:

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface AppUserRepository extends JpaRepository<AppUser, Long> {
    AppUser findByUsername(@Param("username") String username);
}

@RepositoryRestResource(collectionResourceRel = "groups", path = "groups")
public interface UserGroupRepository extends JpaRepository<UserGroup, Long> {
    List<UserGroup> findByName(@Param("name") String name);
}

Below you can see how the relation is defined:

AppUser.java

@Column(name="user_group_id")
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "app_user__user_group",
        joinColumns = {
                @JoinColumn(name = "app_user_id", nullable = false, updatable = false) },
        inverseJoinColumns = {
                @JoinColumn(name = "user_group_id", nullable = false, updatable = false)})
private Set<UserGroup> groups;

UserGroup.java

@Column(name = "app_user_id")
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
private Set<AppUser> users;

Note:

If I pass just a string, not a list for users:

POST http://localhost:8080/groups

{
  "name": "Group 1", 
  "users": "http://localhost:8080/users/1"
}

what I get is:

{
  "cause": {
    "cause": null,
    "message": "Cannot deserialize instance of java.util.Set out of VALUE_STRING token at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 7, column: 12] (through reference chain: mahlzeit.api.hibernate.model.UserGroup["users"])"
  },
  "message": "JSON parse error: Cannot deserialize instance of java.util.Set out of VALUE_STRING token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.Set out of VALUE_STRING token at [Source: (org.apache.catalina.connector.CoyoteInputStream); line: 7, column: 12] (through reference chain: mahlzeit.api.hibernate.model.UserGroup["users"])"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try

@Column(name = "app_user_id")
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups",cascade = CascadeType.ALL)
private Set<AppUser> users;

finally pass list of users

{
  "name": "Group 1", 
  "users": ["http://localhost:8080/users/1"]
}

for Cannot deserialize instance of java.util.Set error it expect a list of users not instans of single user


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...