休眠单向一对多关系给了我stackoverflow异常

时间:2018-08-04 04:23:26

标签: java hibernate

几次尝试后,我遇到一个我不明白为什么的错误,但我认为我或多或少是从哪里来的

我有两个实体,规则和活动。 一条规则有很多活动,活动没有有关规则的信息(但是,我必须使其成为双向的,但它给了我太多错误)

如果一个规则有0个活动,并且我对其进行了更新,则不会给我任何错误 如果一个规则有1个或多个活动,而我对其进行了更新,则它会进行更新,但会导致我从休眠状态返回java.lang.stackoverflow时出错

这是两个实体的重要代码

Activity.java

 @Entity
 public class Activity implements Serializable {

  @OneToMany
  @JoinColumn(name="HINT_ID")
  private List<Hint> hints = new ArrayList<Hint>();


  public Activity() {
    super();
  //more attributes, set and getters
  }

Rule.java

@Entity
@Table(name="RULE")
public class Rule implements Serializable {


public Rule() {}
public Rule(Integer id) {
    this.id = id;
}

 @OneToMany(cascade=CascadeType.ALL, mappedBy="joinedRuleActivityId.rule")
 private List<JoinedRuleActivity> joinedRuleActivityList = new ArrayList<JoinedRuleActivity>();




public List<JoinedRuleActivity> getActivities() {
            return joinedRuleActivityList;
    }

 public void addActivity(Activity activity) {
        // Notice a JoinedUserAchievement object
        JoinedRuleActivity joinedRuleActivity = new JoinedRuleActivity(new JoinedRuleActivity.JoinedRuleActivityId(this, activity));
        joinedRuleActivity.setActivity(activity);
        joinedRuleActivityList.add(joinedRuleActivity);
    }
//more set and getters

JoinedRuleActivity.java

@Entity
@Table(name="RULE_ACTIVITY")

public class JoinedRuleActivity {
  public JoinedRuleActivity() {}

    public JoinedRuleActivity(JoinedRuleActivityId joinedRuleActivityId) {
        this.joinedRuleActivityId = joinedRuleActivityId;
    }

    @ManyToOne
    @JoinColumn(name="ACTIVITY_ID", insertable=false, updatable=false)
    private Activity activity;

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    @EmbeddedId
    private JoinedRuleActivityId joinedRuleActivityId;


    @Embeddable
    public static class JoinedRuleActivityId implements Serializable {

        private static final long serialVersionUID = 2015726292052854803L;




        @ManyToOne
        @JoinColumn(name="RULE_ID")
        private Rule rule;

        @ManyToOne
        @JoinColumn(name="ACTIVITY_ID")
        private Activity activity;

        // required no arg constructor
        public JoinedRuleActivityId() {}

        public JoinedRuleActivityId(Rule rule, Activity activity) {
            this.rule = rule;
            this.activity = activity;
        }

        public JoinedRuleActivityId(Integer ruleId, Integer activityId) {
            this(new Rule(ruleId), new Activity(activityId));
        }

        public Rule getRule() {
            return rule;
        }


        public Activity getActivity() {
            return activity;
        }

        public void setRule(Rule rule) {
            this.rule = rule;
        }

        public void setActivity(Activity activity) {
            this.activity = activity;
        }
        @Override

        public boolean equals(Object instance) {
            if (instance == null)
                return false;

            if (!(instance instanceof JoinedRuleActivityId))
                return false;

            final JoinedRuleActivityId other = (JoinedRuleActivityId) instance;
            if (!(rule.getId().equals(other.getRule().getId())))
                return false;

            if (!(activity.getId().equals(other.getActivity().getId())))
                return false;

            return true;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 47 * hash + (this.rule != null ? this.rule.hashCode() : 0);
            hash = 47 * hash + (this.activity != null ? this.activity.hashCode() : 0);
            return hash;
        }
    }

   }

我认为该错误应该与我遇到的错误非常相似,并且无法通过双向关系解决,但是我不明白这里发生了什么

编辑。

这是我得到的堆栈跟踪错误的一部分,其余的就是一遍又一遍地重复

在org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1240)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1123)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.type.EntityType.resolve(EntityType.java:464)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.type.EntityType.resolve(EntityType.java:457)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.type.ComponentType.resolve(ComponentType.java:688)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:863)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:719)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.processResultSet(Loader.java:991)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.doQuery(Loader.java:949)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:311)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.Loader.loadEntity(Loader.java:2282)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:61)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:51)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]     在org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4121)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]

0 个答案:

没有答案