Spring / JPA:没有循环引用的相同实体之间的多个关系

时间:2018-03-07 10:04:24

标签: jpa spring-boot many-to-many circular-reference

大家早上好。我试图构建一个spring应用程序,它在同一个实体之间有两个ManyToMany关系:一个团队有几个成员和领导者。

第一个实体:

@Entity
@Table(name = "team")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Team implements Serializable {

private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "team2member", joinColumns = @JoinColumn(name = "team_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
    private Set<User> members = new HashSet<User>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "team2leader", joinColumns = @JoinColumn(name = "team_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
    private Set<User> leaders = new HashSet<User>();

    // Members (ManyToMany)
    public Set<User> getMembers() {
        return members;
    }

    public void setMembers(Set<User> members) {
        this.members = members;
    }

    public void addMember(User member) {
        this.members.add(member);
    }

    public void removeMember(User member) {
        this.members.remove(member);
    }

    // Leaders (ManyToMany)
    public Set<User> getLeaders() {
        return leaders;
    }

    public void setLeaders(Set<User> leaders) {
        this.leaders = leaders;
    }

    public void addLeader(User leader) {
        this.leaders.add(leader);
    }

    public void removeLeader(User leader) {
        this.members.remove(leader);
    }
}

第二实体:

@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "team2member", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "team_id", referencedColumnName = "id"))
    private Set<Team> teams = new HashSet<Team>();

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "team2leader", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "team_id", referencedColumnName = "id"))
    private Set<Team> leadedTeams = new HashSet<Team>();

    // Teams (ManyToMany)
    public Set<Team> getTeams() {
        return teams;
    }

    public void setTeams(Set<Team> teams) {
        this.teams = teams;
    }

    public void addTeam(Team team) {
        this.teams.add(team);
    }

    public void removeTeam(Team team) {
        this.teams.remove(team);
    }

    // LeadedTeams (ManyToMany)
    public Set<Team> getLeadedTeams() {
        return leadedTeams;
    }

    public void setLeadedTeams(Set<Team> leadedTeams) {
        this.leadedTeams = leadedTeams;
    }

    public void addLeadedTeam(Team leadedTeam) {
        this.leadedTeams.add(leadedTeam);
    }

    public void removeLeadedTeam(Team leadedTeam) {
        this.leadedTeams.remove(leadedTeam);
    }
}

我的问题:因为我面临一个stackoverflow问题,我想知道这是否有可能。或者可能有更好的解决方案来解决这个问题?

StackTrace(简称):

2018-03-27 01:19:08.635 DEBUG 12656 --- [ XNIO-2 task-12] n.s.aop.logging.LoggingAspect            : Exit: net.schwungkraft.service.DocumentService.findAllForOrderHeader() with result = Page 1 of 0 containing UNKNOWN instances
2018-03-27 01:19:08.636 DEBUG 12656 --- [ XNIO-2 task-12] n.s.aop.logging.LoggingAspect            : Exit: net.schwungkraft.web.rest.OrderHeaderResource.getDocumentsForOrderHeader() with result = <200 OK,[],{X-Total-Count=[0], Link=[</api/order-headers/%7Bid%7D/documents?page=0&size=20>; rel="last",</api/order-headers/%7Bid%7D/documents?page=0&size=20>; rel="first"]}>
2018-03-27 01:19:08.645 DEBUG 12656 --- [ XNIO-2 task-13] n.s.aop.logging.LoggingAspect            : Exit: net.schwungkraft.service.OrderHeaderService.findOneForCurrentUser() with result = OrderHeader{id=3102, orderScopeType='BEWERBUNG', dateStart='null', dateEnd='null', duration=null, classType='null', estimatedNoOfApplicants=null, isTaxLiable='false', amountWithoutTaxes=null, taxesPercent=null, taxesAmount=null, amountWithTaxes=null, extraGrantApplicants=null, extraGrantOthers=null, reminderDate='null', rating='null', ratingText='null', category='null', freeText='null', histComissionRateTitle=null, histPaymentTermDescription='3D', histPaymentTimeLimit=null, histPaymentTermTimeLimitForDiscount=null, histPaymentTermDiscount=null, histContactName='test', histContactStreet='null', histContactPostCode=null, histContactCity='null', histContactCountry='null', histContactContactPerson='null', histContactEmail='null', histContactPhoneNumber='null'}
2018-03-27 01:19:08.645 DEBUG 12656 --- [ XNIO-2 task-13] n.s.aop.logging.LoggingAspect            : Exit: net.schwungkraft.web.rest.OrderHeaderResource.getOrderHeader() with result = <200 OK,OrderHeader{id=3102, orderScopeType='BEWERBUNG', dateStart='null', dateEnd='null', duration=null, classType='null', estimatedNoOfApplicants=null, isTaxLiable='false', amountWithoutTaxes=null, taxesPercent=null, taxesAmount=null, amountWithTaxes=null, extraGrantApplicants=null, extraGrantOthers=null, reminderDate='null', rating='null', ratingText='null', category='null', freeText='null', histComissionRateTitle=null, histPaymentTermDescription='3D', histPaymentTimeLimit=null, histPaymentTermTimeLimitForDiscount=null, histPaymentTermDiscount=null, histContactName='test', histContactStreet='null', histContactPostCode=null, histContactCity='null', histContactCountry='null', histContactContactPerson='null', histContactEmail='null', histContactPhoneNumber='null'},{}>
2018-03-27 01:19:09.162 ERROR 12656 --- [raft-Executor-2] n.s.c.audit.AsyncEntityAuditEventWriter  : Exception while getting entity ID and content {}

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.hibernate.collection.internal.PersistentSet[1]->net.schwungkraft.domain.User["leadedTeams"]->org.hibernate.collection.internal.PersistentSet[0]->net.schwungkraft.domain.Team["members"]->org.hibernate.collection.internal.PersistentSet[1]->net.schwungkraft.domain.User["leadedTeams"]->org.hibernate.collection.internal.PersistentSet[0]->net.schwungkraft.domain.Team["members"]->org.hibernate.collection.internal.PersistentSet[1]->net.schwungkraft.domain.User["leadedTeams"]->org.hibernate.collection.internal.PersistentSet[0]->net.schwungkraft.domain.Team["members"]->org.hibernate.collection.internal.PersistentSet[1]->net.schwungkraft.domain.User["leadedTeams"]->org.hibernate.collection.internal.PersistentSet[0]->net.schwungkraft.domain.Team["members"])
        at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:705)
        at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
        at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)

0 个答案:

没有答案