即使使用JsonIdentifyInfo之后也可以无限递归

时间:2019-07-19 10:27:33

标签: hibernate spring-boot jpa

我遇到无限递归问题。我正在使用JsonIgnore(很好),但是我需要在UsersDocuments类中包括USER对象。当我删除“ JSONignore”注释时,它将返回序列化问题。如果我使用该注释,那就可以了。 我尝试使用JsonpropertyInfo,但这对解决我的问题也无济于事。

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=User.class)
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "first_name")
    private String firstName;
    private String surname;
    private String email;
    @JsonIgnore
    private String password;

    @ManyToMany
    @JoinTable(
            name = "users_roles",
            joinColumns = @JoinColumn(
                    name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(
                    name = "role_id", referencedColumnName = "id"))

    private List<Role> roles;


    @JsonIgnore
    @OneToMany(mappedBy = "user", orphanRemoval = true, cascade= CascadeType.ALL)
    private Set<UsersDocuments> usersDocuments;

    @JsonIgnore
    @OneToMany(mappedBy = "user")
    private List<Event> events;


    @JsonIgnore
    @OneToMany(mappedBy="user")
    private List<Document> sharedDocuments;


    public User() {

    }

    public User(String firstName, String surname, String email, String password) {
        this.firstName = firstName;
        this.surname = surname;
        this.email = email;
        this.password = password;
    }

    public Set<UsersDocuments> getUsersDocuments() {
        return usersDocuments;
    }

    public void setUsersDocuments(Set<UsersDocuments> usersDocuments) {
        this.usersDocuments = usersDocuments;
    }

    public void addDocument(Document document) {
        UsersDocuments usersDocuments = new UsersDocuments(this, document);
        this.usersDocuments.add(usersDocuments);
        document.getDocumentsForUsers().add(usersDocuments);
    }

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

    public long getId() {
        return id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email){this.email = email;}

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public List<Document> getSharedDocuments() {
        return sharedDocuments;
    }

    public void setSharedDocuments(List<Document> sharedDocuments) {
        this.sharedDocuments = sharedDocuments;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
        User user = (User) o;
        return getId() == user.getId();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId());
    }

    @Override
    public String toString() {
        return "User{" +
                "firstName='" + firstName + '\'' +
                ", surname='" + surname + '\'' +
                ", email='" + email + '\'' +
                ", roles=" + roles +
                '}';
    }

    public List<Event> getEvents() {
        return events;
    }

    public void setEvents(List<Event> events) {
        this.events = events;
    }
}

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Document.class)
public class Document {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(unique = true)
    private String name;

    private String title;

    private String description;

    @JsonIgnore
    @Column(name = "resource_path")
    private String resourcePath;

    @Column(name = "upload_datetime", columnDefinition = "DATETIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date uploadDatetime;

    @OneToMany(mappedBy = "document", cascade= CascadeType.ALL, orphanRemoval = true)
    private Set<UsersDocuments> documentsForUsers = new HashSet<>();

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


    public Document() {
}


    public Document(String title, String description){
        this.title = title;
        this.description = description;
    }

    public long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getResourcePath() {
        return resourcePath;
    }

    public void setResourcePath(String resourcePath) {
        this.resourcePath = resourcePath;
    }


    @Override
    public String toString() {
        return "Document{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", resourcePath='" + resourcePath + '\'' +
                ", uploadDatetime=" + uploadDatetime + '\'' +
                ". user=" + user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Document)) return false;
        Document document = (Document) o;
        return getId() == document.getId();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId());
    }

    public Date getUploadDatetime() {
        return uploadDatetime;
    }

    public void setUploadDatetime(Date uploadDatetime) {
   //     Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(uploadDatetime.toString());
        this.uploadDatetime = uploadDatetime;
    }

    public User getUser() {
        return user;
    }

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

    public Set<UsersDocuments> getDocumentsForUsers() {
        return documentsForUsers;
    }

    public void setDocumentsForUsers(Set<UsersDocuments> documentsForUsers) {
        this.documentsForUsers = documentsForUsers;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String authority;

    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    private List<User> users;

    public Role(){

    }

    public Role(long id, String authority){
        this.id = id;
        this.authority = authority;
    }

    public long getId() {
        return id;
    }

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

    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return authority;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Role)) return false;
        Role role = (Role) o;
        return getId() == role.getId();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId());
    }

}

@Entity
@Table(name="users_documents")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=UsersDocuments.class)
public class UsersDocuments {

    @EmbeddedId
    private UserDocumentsId id;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("user")
    @JoinColumn(name = "user_id")
    // IF I JSONIGNORE THIS, IT WORKS, BUT I NEED TO INCLUDE IT IN JSON
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("document")
    @JoinColumn(name = "document_id")
    @JsonIgnore
    private Document document;

    @Column(name="approval")
    private int approval = 2;

    public UsersDocuments(){
    }

    public UsersDocuments(User user, Document document) {
        this.user = user;
        this.document = document;
        this.id = new UserDocumentsId(user.getId(), document.getId());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof UsersDocuments)) return false;
        UsersDocuments that = (UsersDocuments) o;
        return getUser().equals(that.getUser()) &&
                getDocument().equals(that.getDocument());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getUser(), getDocument());
    }

    public UserDocumentsId getId() {
        return id;
    }

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

    public User getUser() {
        return user;
    }

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

    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    public int getApproval() {
        return approval;
    }

    public void setApproval(int approval) {
        this.approval = approval;
    }
}

@Embeddable
public class UserDocumentsId implements Serializable {

    @Column(name="user_id")
    private long user;

    @Column(name="document_id")
    private long document;

    public UserDocumentsId(){}

    public UserDocumentsId(long user, long document) {
        this.user = user;
        this.document = document;
    }

    public long getUser() {
        return user;
    }

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

    public long getDocument() {
        return document;
    }

    public void setDocument(long document) {
        this.document = document;
    }
}

0 个答案:

没有答案