- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
除非提供了某个执行参数,否则如何配置他们的 JPA 实体以不获取相关实体。
根据 Spring 的文档,4.3.9. Configuring Fetch- and LoadGraphs ,您需要使用 @EntityGraph
注释来指定查询的获取策略,但这并不能让我在运行时决定是否要加载这些实体。
我可以在单独的查询中获取子实体,但为了做到这一点,我需要将我的存储库或实体配置为不检索任何子实体。不幸的是,我似乎找不到任何关于如何做到这一点的策略。 FetchPolicy
被忽略,EntityGraph
仅在指定我想要急切检索的实体时才有用。
例如,假设 Account
是父级,Contact
是子级,并且一个 Account 可以有多个联系人。
我希望能够做到这一点:
if(fetchPolicy.contains("contacts")){
account.setContacts(contactRepository.findByAccountId(account.getAccountId());
}
问题是 spring-data 无论如何都急切地获取联系人。
Account Entity 类如下所示:
@Entity
@Table(name = "accounts")
public class Account
{
protected String accountId;
protected Collection<Contact> contacts;
@OneToMany
//@OneToMany(fetch=FetchType.LAZY) --> doesn't work, Spring Repositories ignore this
@JoinColumn(name="account_id", referencedColumnName="account_id")
public Collection<Contact> getContacts()
{
return contacts;
}
//getters & setters
}
AccountRepository 类如下所示:
public interface AccountRepository extends JpaRepository<Account, String>
{
//@EntityGraph ... <-- has type= LOAD or FETCH, but neither can help me prevent retrieval
Account findOne(String id);
}
最佳答案
如果没有调用 getContacts() 产生的对象方法,则延迟获取应该正常工作。
如果您喜欢更多的手动工作,并且真的希望对此进行控制(可能更多的上下文取决于用例)。我建议您从帐户实体中删除联系人,然后将帐户映射到联系人中。告诉 hibernate 忽略该字段的一种方法是使用 @Transient 注释对其进行映射。
@Entity
@Table(name = "accounts")
public class Account
{
protected String accountId;
protected Collection<Contact> contacts;
@Transient
public Collection<Contact> getContacts()
{
return contacts;
}
//getters & setters
}
然后在您的服务类中,您可以执行以下操作:
public Account getAccountById(int accountId, Set<String> fetchPolicy) {
Account account = accountRepository.findOne(accountId);
if(fetchPolicy.contains("contacts")){
account.setContacts(contactRepository.findByAccountId(account.getAccountId());
}
return account;
}
希望这是您正在寻找的。顺便说一句,代码未经测试,因此您可能应该再次检查。
关于java - Spring Data JPARepository : How to conditionally fetch children entites,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266952/
我想同时创建两个实体,我只想保存其中一个。有人知道怎么做吗?我尝试了多个托管对象上下文,但它不起作用。 最佳答案 为实现您的要求,请在按下保存按钮之前不要创建实体实例。将所有输入的数据存储到其他东西中
是否可以比较当前“脏”版本(其某些属性已更改但尚未持久化的对象)和“原始”版本(数据仍在数据库中)之间的实体对象的状态. 我的假设是我可以有一个“脏”对象,然后从数据库中提取一个新对象并比较两者。例如
当我尝试为实体创建原则 crud 时,我收到“未知实体 namespace 别名”异常。 我有以下项目结构 使用 src\Project\Entity 目录中的实体的一系列 bundle (在 Bun
在我的查询中,我需要返回一个没有默认构造函数的类的实例(特别是这是在自定义成员资格提供程序中,而 MembershipUser 是罪魁祸首) var users = from l in context
我正在开发 MVC 5 应用程序。最初我使用单一数据库和 EF6 数据库优先方法,我使用我的 DbContext 实例访问我的数据库,它有 102 个表。我将其实例声明为: private MyEnt
我想使用选择来获取带有 Doctrines QueryBuilder 的实体数组。但我需要一个 ORDER BY,它使用一个外部属性(与外键相关的表中的属性)。我想直观地写的是这样的: $repo =
除非提供了某个执行参数,否则如何配置他们的 JPA 实体以不获取相关实体。 根据 Spring 的文档,4.3.9. Configuring Fetch- and LoadGraphs ,您需要使用
让我们假设我有以下实体:。现在,我希望获得所有用户(使用Pages)的兴趣爱好,而不使用@OneToMany,并映射到名为SearchUserModel的新类:。当我调用下面的存储库方法时,Sprin
让我们假设我有以下实体:。现在,我希望获得所有用户(使用Pages)的兴趣爱好,而不使用@OneToMany,并映射到名为SearchUserModel的新类:。当我调用下面的存储库方法时,Sprin
我是一名优秀的程序员,十分优秀!