在我通过以下方式添加字节码检测之前,我没有在任何一个类上进行延迟加载:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<tasks>
<taskdef name="instrument" classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
<classpath>
<path refid="maven.runtime.classpath" />
<path refid="maven.plugin.classpath" />
</classpath>
</taskdef>
<instrument verbose="false">
<fileset dir="${project.build.outputDirectory}">
<include name="**/db/**/*.class" />
</fileset>
</instrument>
</tasks>
</configuration>
</plugin>
这是我的两个实体类,大大减少了:
此表“来自Ininvgrmtr”按我的意愿工作(没有n + 1期):
@Entity
@Table(name = "ININVGRMTR", catalog = "CO05IN", schema = "")
@XmlRootElement
public class Ininvgrmtr implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 8)
@Column(name = "IGMGRUP", nullable = false, length = 8)
private String igmgrup;
//other attributes
@JoinColumn(name = "IGMGRUP", referencedColumnName = "IGGRUP", nullable = false, insertable = false, updatable = false)
@OneToOne(optional = false, fetch=FetchType.LAZY)
private Ininvgrp ininvgrp;
}
此表“来自Ininvgrp”不会:
@Entity
@Table(name = "ININVGRP", catalog = "CO05IN", schema = "")
@XmlRootElement
public class Ininvgrp implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 8)
@Column(name = "IGGRUP", nullable = false, length = 8)
private String iggrup;
//other attributes
@OneToOne(cascade = CascadeType.ALL, mappedBy = "ininvgrp", fetch=FetchType.LAZY)
private Ininvgrmtr ininvgrmtr;
//getters setters
}
说明问题:
entityManagerFactory.createEntityManager().createQuery("from Ininvgrmtr").getResultList();
将以下内容打印到日志中(这很好):
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_ from CO05IN.ININVGRMTR ininvgrmtr0_
虽然
entityManagerFactory.createEntityManager().createQuery("from Ininvgrp").getResultList();
打印打印以下内容:
INFO: Hibernate: select ininvgrp0_.IGGRUP as IGGRUP97_, ininvgrp0_.Added as Added97_, ininvgrp0_.IGABCF as IGABCF97_, ininvgrp0_.IGADCN as IGADCN97_, ininvgrp0_.IGADDT as IGADDT97_, ininvgrp0_.IGADUS as IGADUS97_, ininvgrp0_.IGCAT as IGCAT97_, ininvgrp0_.IGDESC as IGDESC97_, ininvgrp0_.IGMDCN as IGMDCN97_, ininvgrp0_.IGMDDT as IGMDDT97_, ininvgrp0_.IGMDUS as IGMDUS97_, ininvgrp0_.IGRETH as IGRETH97_, ininvgrp0_.IGSTA as IGSTA97_, ininvgrp0_.IGTYPE as IGTYPE97_, ininvgrp0_.IGUBAS as IGUBAS97_, ininvgrp0_.IGUSEL as IGUSEL97_, ininvgrp0_.IGUWGT as IGUWGT97_, ininvgrp0_.Modified as Modified97_ from CO05IN.ININVGRP ininvgrp0_
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
INFO: Hibernate: select ininvgrmtr0_.IGMGRUP as IGMGRUP96_0_, ininvgrmtr0_.IGMTRACE as IGMTRACE96_0_ from CO05IN.ININVGRMTR ininvgrmtr0_ where ininvgrmtr0_.IGMGRUP=?
...
造成这种情况的原因是什么?
答案 0 :(得分:1)
Ininvgrmtr
上的属性被标记为不可为空且不是可选的,因此hibernate知道必须有一个具有给定id的实体。然后,Hibernate可以创建动态代理并将其设置为属性值。然后,只有在访问其属性时才会初始化此代理。
另一方面,从Ininvgrp
开始,默认情况下该属性被标记为可选。在这种情况下,Hibernate不能使用动态代理,因为如果没有匹配的实体,它必须返回null。
使用字节码编织可以修改对字段本身的每次访问以查询数据库。如果您可以在构建过程中使用字节码检测,那么这将是首选解决方案。
另一种解决方法是将属性声明为OneToMany
关系,并将空/一个元素列表转换为null或其在getter和setter中的第一个元素,如下所示:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "ininvgrp", fetch=FetchType.LAZY)
private List<Ininvgrmtr> ininvgrmtr;
public void setIninvgrmtr(Ininvgrmtr ininvgrmtr) {
if (this.ininvgrmtr == null || this.ininvgrmtr.isEmpty()) {
this.ininvgrmtr = Collections.singletonList(ininvgrmtr);
} else {
this.ininvgrmtr.set(0, ininvgrmtr);
}
}
public Ininvgrmtr getIninvgrmtr() {
return ininvgrmtr == null || ininvgrmtr.isEmpty() ? null : ininvgrmtr.get(0);
}
修改:this blog post可以找到有关某些参考文献问题的更详细说明。