Java SpringBoot JPA获取/加载图
我有两个实体Java SpringBoot JPA获取/加载图,java,jpa,spring-boot,eclipselink,Java,Jpa,Spring Boot,Eclipselink,我有两个实体 @Entity @Table(name = "LOCALIZATION") @NamedQuery(name = "getByCountry, query = "SELECT ld FROM LocalizationDefinition ld WHERE ld.country = :country") @NamedEntityGraph(name = "loadAll", attributeNodes = { @NamedAttributeNode(value =
@Entity
@Table(name = "LOCALIZATION")
@NamedQuery(name = "getByCountry,
query = "SELECT ld FROM LocalizationDefinition ld WHERE ld.country = :country")
@NamedEntityGraph(name = "loadAll",
attributeNodes = { @NamedAttributeNode(value = "localizations") })
public class LocalizationDefinition {
@Id
@SequenceGenerator(name = "LocalizationSequence", sequenceName = "LOCALIZATION_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "LocalizationSequence")
private Integer id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "localizationDefinition")
private List<LocalizationEntry> localizations;
...
}
存储库摘录
public List<LocalizationDefinition> getByCountry(final String country) {
final TypedQuery<LocalizationDefinition> query = em
.createNamedQuery("getByCountry", LocalizationDefinition.class);
query.setParameter("country", country);
query.setHint("javax.persistence.loadgraph", em.getEntityGraph("loadAll"));
final List<LocalizationDefinition> allLocalizations = query.getResultList();
return allLocalizations;
}
公共列表getByCountry(最终字符串国家){
最终类型dquery query=em
.createNamedQuery(“getByCountry”,LocalizationDefinition.class);
query.setParameter(“国家”,国家);
setHint(“javax.persistence.loadgraph”,em.getEntityGraph(“loadAll”);
最终列表allLocalizations=query.getResultList();
返回所有本地化;
}
我有一个自定义存储库实现,它使用命名查询和实体图加载国家/地区的所有条目。我将Spring Boot 1.4与嵌入式Tomcat一起使用,并尝试了EclipseLink 2.5.2和2.6.3。我的期望是,只创建1个SQL查询,但最终会出现n+1查询问题。我敢肯定,在一个月前WebLogicServer12c中的JEE7测试应用程序中,这一点与预期一样有效
如果我做错了什么或者理解错了什么,有什么想法吗
完整持久性.xml 首先我想要一个2.0版本,但将其更新到2.1并不会改变任何事情
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="MatCommonEntity">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.target-database"
value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform"/>
<property name="eclipselink.query-results-cache" value="false"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
假的
启用\u选择性
eclipselink.weaving在JPA配置类中设置为static。我还在pom.xml中启用了编译时的静态编织
设置query.setHint(QueryHints.FETCH,“ld.localizations”);在自定义存储库中,实现解决了这个问题。但是这是特定于提供程序的,设置loadgraph应该已经完成了这项工作。您是如何定义获得N+1的?另外,显示您的persistence.xmlpersistence.xml false启用布局的。很难添加格式化为注释的内容。我已经在SpringBoot中启用了JPA/SQL日志,并在日志中看到了SQL查询。我发现的一个解决方案是使用一个特殊的eclipselink提示:query.setHint(QueryHints.FETCH,“ld.localizations”);但是,这是特定于提供程序的,loadgraph应该已经完成了这项工作。您是如何定义获得N+1的?另外,请显示persistence.xmlpersistence.xml false以启用布局的。很难添加格式化为注释的内容。我已经在SpringBoot中启用了JPA/SQL日志,并在日志中看到了SQL查询。我发现的一个解决方案是使用一个特殊的eclipselink提示:query.setHint(QueryHints.FETCH,“ld.localizations”);但这是特定于提供者的,loadgraph应该已经完成了这项工作。
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="MatCommonEntity">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.target-database"
value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform"/>
<property name="eclipselink.query-results-cache" value="false"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>