- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看过各种帖子,描述 JPA EntityGraph 允许在运行时选择图表。我不太清楚这指的是什么。
出于善意和尊重,我想引用这篇有用的文章:https://www.baeldung.com/jpa-entity-graph 。 (大多数 JPA 用户可能已经经历过它。)
文章引用 -
EntityGraph allows grouping the related persistence fields which wewant to retrieve and lets us choose the graph type at runtime.
并在结论部分再次巩固了上述陈述。
In this article, we've explored using the JPA Entity Graph todynamically fetch an Entity and its associations.
The decision is made at runtime in which we choose to load or not therelated association.
正如我们在文章 (5.1) 中看到的 - EntityGraph 使用注释定义如下 -
5.1。使用注释定义实体图
@NamedEntityGraph(
name = "post-entity-graph",
attributeNodes = {
@NamedAttributeNode("subject"),
@NamedAttributeNode("user"),
@NamedAttributeNode("comments"),
}
)
@Entity
public class Post {
@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();
//...
}
@NameEntityGraph 注释是在编译时定义的,我在这里没有看到任何运行时或动态的内容。
但在 5.2 中 - 实体图是使用 API 或以编程方式定义的 -
5.2。使用 JPA API 定义实体图
EntityGraph<Post> entityGraph = entityManager.createEntityGraph(Post.class);
entityGraph.addAttributeNodes("subject");
entityGraph.addAttributeNodes("user");
在 5.2 方法中,我看到可以使用某种逻辑动态选择节点。这种方法就是所谓的“动态获取”和“基于运行时”。或者我错过了什么,我还有更多需要理解的吗?
进一步采用6 中给出的方法。使用实体图
例如:
EntityGraph entityGraph = entityManager.getEntityGraph("post-entity-graph");
Map<String, Object> properties = new HashMap<>();
properties.put("javax.persistence.fetchgraph", entityGraph);
Post post = entityManager.find(Post.class, id, properties);
都是编程性的,因此可以在运行时更改,即它们可以说是动态的。
但是上面的文章中遗漏了一种方法,但在这里提到了 - https://www.baeldung.com/spring-data-jpa-named-entity-graphs如下所示,似乎不符合动态标准。
public interface ItemRepository extends JpaRepository<Item, Long> {
@EntityGraph(value = "Item.characteristics")
Item findByName(String name);
}
动态方法是否仅指 5.2 风格,或者甚至也暗示 5.1 风格。
最佳答案
您不能将动态实体图与 spring-data 一起使用,因为 JpaRepository
没有方法来传递实体图,例如
Optional<T> findById(ID id, EntityGraph entityGraph);
使用自定义 JPA 存储库
您可以通过创建自定义存储库并通过 EntityManager
使用实体图来使用原始 JPA。
使用 spring-data-jpa-entity-graph
使用库 spring-data-jpa-entity-graph 有一种更方便的方法.
它允许将 findById()
或 findByName()
等 JPA 存储库方法与动态实体图一起使用。
我更喜欢将它与这个辅助类一起使用
public abstract class EntityGraphBuilder<T> {
private List<String> result = new ArrayList<>();
protected T self;
public T add(String path) {
result.add(path);
return self;
}
public DynamicEntityGraph build() {
return new DynamicEntityGraph(EntityGraphType.FETCH, result);
}
}
每个实体都有自己的GraphBuilder
@Entity
public class OrderEntity {
@Id
private Long id;
@Column
private name;
@ManyToOne(fetch = FetchType.LAZY)
private OrderRequestEntity orderRequest;
@ManyToOne(fetch = FetchType.LAZY)
private ProviderEntity provider;
public static GraphBuilder graph() {
return new GraphBuilder();
}
public static class GraphBuilder extends EntityGraphBuilder<GraphBuilder> {
private GraphBuilder() {
self = this;
}
public GraphBuilder orderRequest() {
return add("orderRequest");
}
public GraphBuilder provider() {
return add("provider");
}
}
}
存储库使用 spring-data-jpa-entity-graph
库中的 EntityGraphJpaRepository
@Repository
public interface OrdersRepository extends EntityGraphJpaRepository<OrderEntity, Long> {
OrderEntity findByName(String name, EntityGraph entityGraph);
}
您也可以将 findByName()
等派生查询方法与动态实体图结合使用。
使用findById()
方法的示例,相同的方法可以应用于findByName()
OrdersRepository ordersRepository;
Long orderId = 1L;
OrderEntity order = ordersRepository.findById(
orderId,
OrderEntity.graph().orderRequest().provider().build()
).orElseThrow(
() -> new ServiceException("Can't find orderId=" + orderId)
);
关于java - JPA EntityGraph 如何允许在运行时选择实体图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71175593/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!