多对多复合键映射无限循环

时间:2015-08-12 20:35:28

标签: java spring hibernate jpa

Hibernate进入无限循环并点击DB直到Stackoverflow。当我想在Hibernate中设置多对多的设置时,如何解决这个问题?

这些是DB Model类:

帐户 - 多对多关系的一方

@Entity
@Table(name = "account")
public class Account {
    @Id
    @GeneratedValue
    @Column(name = "account_id")
    private Long accountId;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "id.account", cascade = CascadeType.ALL)
    private Set<AccountAppuser> accountAppusers = new HashSet<AccountAppuser>(0);
    .. getters, setters, hashCode, equals
}

Appuser - 多对多关系的另一面

@Entity
@Table(name = "appuser")
@SuppressWarnings("serial")
public class Appuser {
    @Id
    @GeneratedValue
    @Column(name = "appuser_id")
    private Long appuserId;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "id.appuser", cascade = CascadeType.ALL)
    private Set<AccountAppuser> accountAppusers = new HashSet<AccountAppuser>(0);
    .. getters, setters, hashCode, equals
}

AccountAppuser - 复合键

@Entity
@Table(name = "account_appuser")
@AssociationOverrides({ @AssociationOverride(name = "id.account", joinColumns = @JoinColumn(name = "account_id") ), @AssociationOverride(name = "id.appuser", joinColumns = @JoinColumn(name = "appuser_id") ) })
@SuppressWarnings("serial")
public class AccountAppuser {
    @EmbeddedId
    private AccountAppuserId id;
    private String access;
    .. getters, setters, hashCode, equals
}

AccountAppuserId - 复合键ID bean

@Embeddable
@SuppressWarnings("serial")
public class AccountAppuserId implements Serializable {
    @ManyToOne
    private Account account;
    @ManyToOne
    private Appuser appuser;
    .. getters, setters, hashCode, equals
}

这些是我的表格和数据 帐户

account_id
1
2
3
4

APPUSER

appuser_id
11
12
13
14

account_appuser

account_id  appuser_id
1       11
1       12
1       13
1       14
2       11
2       12
2       13
2       14

这是 DAO 代码

    public class AccountRepo {

        @Transactional(readOnly = true)
        public List<Account> getAllAccounts(){
            return this.sessionFactory.getCurrentSession().createCriteria(Account.class).list();
        }
    }

输出我得到了:

Hibernate: 
    select
        this_.account_id as account_1_0_1_,
        accountapp2_.account_id as account_2_0_3_,
        accountapp2_.account_id as account_2_1_3_,
        accountapp2_.appuser_id as appuser_3_1_3_,
        accountapp2_.account_id as account_2_1_0_,
        accountapp2_.appuser_id as appuser_3_1_0_,
        accountapp2_.access as access1_1_0_ 
    from
        account this_ 
    left outer join
        account_appuser accountapp2_ 
            on this_.account_id=accountapp2_.account_id
Hibernate: 
    select
        account0_.account_id as account_1_0_0_,
        accountapp1_.account_id as account_2_0_1_,
        accountapp1_.account_id as account_2_1_1_,
        accountapp1_.appuser_id as appuser_3_1_1_,
        accountapp1_.account_id as account_2_1_2_,
        accountapp1_.appuser_id as appuser_3_1_2_,
        accountapp1_.access as access1_1_2_ 
    from
        account account0_ 
    left outer join
        account_appuser accountapp1_ 
            on account0_.account_id=accountapp1_.account_id 
    where
        account0_.account_id=?

上面的第二个查询将永远存在。

0 个答案:

没有答案