java 实体图在实体层中不起作用,但在存储库层中起作用

wfsdck30  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(141)

我有实体链用户-〉加密钱包-〉列表〈加密计数
1.

public class User {

    @Id
    @Column(name = "id")
    @GenericGenerator(name = "uuidFromIp" , strategy = "org.hibernate.id.uuid.CustomVersionOneStrategy")
    @GeneratedValue(generator = "uuid_gen_strategy")
    private UUID id;

    @Column(name = "user_name")
    private String userName;

    @Column(name = "email")
    private String email;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "crypto_wallet_id")
    private CryptoWallet cryptoWallet;
}
public class CryptoWallet {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "balance")
    private BigDecimal balance;

    @OneToMany(mappedBy = "cryptoWallet", cascade = CascadeType.ALL)
    private List<CryptoCount> cryptoCounts;

    @OneToOne(mappedBy = "cryptoWallet")
    private User walletOwner;
}
public class CryptoCount {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "crypto_id")
    private CryptoCurrency cryptoCurrency;

    @Column(name = "count")
    private BigDecimal countOfCrypto;

    @ManyToOne
    @JoinColumn(name = "crypto_wallet_id")
    private CryptoWallet cryptoWallet;
}

我的任务是在一个请求中按CryptoWalletOwnerId加载所有CryptoCount
当我使用下一个方法在仓库层一切正常工作(与一个查询)。

@EntityGraph(attributePaths = {"cryptoCurrency","cryptoWallet","cryptoWallet.walletOwner"})
List<CryptoCount> findAllByCryptoWalletWalletOwnerId(UUID id);
    • 但是**

当我这样注解CryptoCount时:

@NamedEntityGraph(name = "CryptoCountWithCurrencyAndWallet",attributeNodes = {
        @NamedAttributeNode(value = "cryptoCurrency"),
        @NamedAttributeNode(value = "cryptoWallet")
})

加密钱包是这样的:

@NamedEntityGraph(name = "WalletWithCryptoCountsAndWalletOwner", attributeNodes = {
        @NamedAttributeNode(value = "cryptoCounts"),
        @NamedAttributeNode(value = "walletOwner",subgraph = "UserWithWallet")
}, subgraphs = {
        @NamedSubgraph(name = "UserWithWallet", attributeNodes = @NamedAttributeNode("cryptoWallet"))
})

还有这样的用户

@NamedEntityGraph(name = "UserWithWallet",attributeNodes = {
        @NamedAttributeNode("cryptoWallet")
},subgraphs = {
        @NamedSubgraph(name = "userWithWallet", attributeNodes = {@NamedAttributeNode("cryptoWallet")})
})

并使用存储库中的下一个方法:

@EntityGraph(value = "CryptoCountWithCurrencyAndWallet")
    List<CryptoCount> findAllByCryptoWalletWalletOwnerId(UUID id);

我得到了两个数据库查询而不是一个。我的代码有什么问题?

sulc1iza

sulc1iza1#

@NamedEntityGraph(name = "CryptoCountWithCurrencyAndWallet",attributeNodes = {
        @NamedAttributeNode(value = "cryptoCurrency"),
        @NamedAttributeNode(value = "cryptoWallet",subgraph = "withWalletOwner")
},subgraphs = {
        @NamedSubgraph(name = "withWalletOwner",attributeNodes = {@NamedAttributeNode("walletOwner")})
})

CryptoCount中解决了该问题

相关问题