与OneToOne和OneToMany的延迟获取不起作用

时间:2018-07-31 20:31:27

标签: java hibernate spring-boot jpa

我有3个实体,人员,员工,人员电子邮件:

@Entity
@Table(name = "PERSON")
public class Person
{
private String id;
private String lastName;
private String middleName;
private String firstName;
private Employee employee;
private Set<PeopleEmail> peopleEmail;

@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@LazyToOne(value = LazyToOneOption.NO_PROXY)
@JoinColumns({
        @JoinColumn(name = "ID", referencedColumnName = "ID")
})
public Set<PeopleEmail> getPeopleEmail() {
    return peopleEmail;
}

public void setPeopleEmail(Set<PeopleEmail> peopleEmail) {
    this.peopleEmail = peopleEmail;
}

@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "ID", referencedColumnName = "EMPLOYEE_ID")
@LazyToOne(value = LazyToOneOption.NO_PROXY)
@Basic(fetch = FetchType.LAZY)
public Employee getEmployee() {
    return employee;
}

public void setEmployee (Employee employee) {
    this.employee = employee;
}
...
}

-

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
  private String employeeId;
  private String spouseFirst;
  private String spouseLast;
  private String supervisorName;

  @Id
  @Column(name = "EMPLOYEE_ID", nullable = false, length = 10)
  public String getEmployeeId() {
   return employeeId;
  }

  public void setEmployeeId(String employeeId) {
   this.employeeId = employeeId;
  }
  ....
}

-

@Entity
@Table(name = "PEOPLE_EMAIL")
public class PeopleEmail
{
private String personEmailTypes;
private String personEmailAddresses;
private String personPreferredEmail;
private PeopleEmailPK peopleEmailPK;
private Long pos;

@EmbeddedId
public PeopleEmailPK getId()
{
    return peopleEmailPK;
}

public void setId(PeopleEmailPK peopleEmailPK)
{
    this.peopleEmailPK = peopleEmailPK;
}

我正在使用的数据库无法更改,并且非常规范化。因此,PeopleEmailPK主键(如果您需要查看此类,我会很乐意提供)

我也将其添加到我的pom.xml文件中:

<plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <version>5.0.12.Final</version>
            <executions>
                <execution>
                    <configuration>
                        <failOnError>true</failOnError>
                        <enableLazyInitialization>true</enableLazyInitialization>
                        <enableDirtyTracking>true</enableDirtyTracking>
                        <enableAssociationManagement>true</enableAssociationManagement>
                        <enableExtendedEnhancement>false</enableExtendedEnhancement>
                    </configuration>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

问题是无论我做什么,都无法让Employee和PeopleEmail延迟加载。当我调用与Person相关的任何东西时,它将运行3个查询。

我在Spring Boot项目中使用Hibernate 5.0.12有何价值?

0 个答案:

没有答案