java - 渴望获取特定对象的特定惰性属性
问题描述
我有以下模型:
@Table(name = "foo")
public class Foo {
@ManyToOne(fetch = FetchType.LAZY)
private Bar bar;
}
Entity Framework
在类似情况下在 .NET 中使用时,我可以急切地将Bar
属性带入以下内容:
context.Foo.Include(f => f.bar).First()
Hibernate有什么等价的吗?
我的情况是我将一个具有惰性属性的对象保存到我的服务器中的会话中。然后当我检索会话属性时,我无法访问休眠会话的惰性属性已经消失了。我不能把这个属性当作是EAGER
因为它是从@MappedSuperclass
许多其他类使用的继承而来的。
谢谢你的帮助。
解决方案
JPA实体图:
@Entity @Table(name = "foo") @NamedEntityGraph(name = "foo.bar", attributeNodes = @NamedAttributeNode("bar") ) public class Foo { @ManyToOne(fetch = FetchType.LAZY) private Bar bar; } Foo foo = entityManager.find( Foo.class, id, Collections.singletonMap( "javax.persistence.fetchgraph", entityManager.getEntityGraph("foo.bar") ) );
您可以在那里看到另一个示例和更详细的说明。
-
@Entity @Table(name = "foo") @FetchProfile( name = "foo.bar", fetchOverrides = { @FetchProfile.FetchOverride( entity = Foo.class, association = "bar", mode = FetchMode.JOIN ) } ) public class Foo { @ManyToOne(fetch = FetchType.LAZY) private Bar bar; } session.enableFetchProfile("foo.bar"); Foo foo = session.byId(Foo.class).load(id);
推荐阅读
- c# - EF Core 3.0 SumAsync 触发聚合函数异常
- c++ - Visual Studio 2017 (C++):在同一解决方案中从项目 B 中的项目 A 访问变量?
- javascript - 检查 CSS 属性的值
- c# - 在 MongoDB C# 驱动程序中插入包含对另一个实体的引用的实体
- java - 面板和按钮已添加到框架中但未出现在框架中
- python - 在python中将命令的输出转换为字典格式
- java - Intellij无法打开maven项目
- python - Python 请求 - 我开始编程
- mysql - 使用 PDO 连接到外部主机时出现奇怪的问题
- uwp - mediainfo.dll 与 UWP 兼容吗?