2つのドメインクラスの双方向マッピングを作成する際に問題があります。Hibernate:双方向ManyToOneマッピング - 逆の関係が動作しない
多くのAccountTransactionsを持つ私のUserAccount.javaがあります。 AccountTransactionドメインオブジェクトには、外部キーを持つ列user_account_idがあります。
私はマッピング次のように設定している:
UserAccount.javaを
// Other properties
@ManyToOne(optional = false)
@JoinColumn(name = "user_account_id")
@NotNull
private UserAccount userAccount;
// Getters and setters...
AccountTransaction.java
@OneToMany(cascade=CascadeType.ALL, mappedBy="userAccount")
public List<AccountTransaction> accountTransactions;
シナリオは、私がしたいということです対応するaccountTransactionsを持つすべてのuserAccountのリストをJSONとして取得する配列ですが、accountTransactionsオブジェクトは常にnullです。私はまた、リポジトリに変更され、クエリの上にそれを試してみました
:私はこのクエリを超える値を取得する場合
@Query("SELECT account FROM UserAccount account JOIN FETCH account.accountTransactions WHERE account.user = :systemUser AND account.adminAccount = TRUE")
List<UserAccount> findAllAdminAccountWithTransactions(@Param("systemUser") User systemUser);
を、それが最初のリポジトリですべてを正しく返します。しかし、それは例外をスローする:
c.d.c.w.rest.errors.ExceptionTranslator : An unexpected error occurred: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.david.coinlender.domain.UserAccount["accountTransactions"]->org.hibernate.collection.internal.PersistentBag[0]->com.david.coinlender.domain.AccountTransaction["userAccount"]
私はどこか無限のループを持っているようだ。誰かが私に解決策を教えてもらえますか?
このrbingsが成功しない場合、結果が返されないか、エラーが返されます。 –
まずはnullだと思っていましたが、コンソールにエラーが表示されるようになりました。私は秒で私の説明を更新します – dave0688