- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Hibernate 新手尝试理解 LAZY 和 EAGER 抓取之间的差异。
地址模型:
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String street;
@Column
private String city;
@OneToOne
private Department department;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Address [id=" + id + ", street=" + street + ", city=" + city + "]";
}
}
部门型号:
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
@OneToOne(mappedBy = "department", fetch = FetchType.LAZY)
private Address address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
测试方法:
@Test
public void testDepartment() {
Long departmentID;
Department department;
Long addressId;
Address address;
try (Session session = SessionUtil.getSession()) {
Transaction tx = session.beginTransaction();
department = new Department();
department.setId(69L);
department.setName("company");
address = new Address();
address.setCity("city");
address.setStreet("street");
address.setDepartment(department);
session.save(department);
session.save(address);
departmentID = department.getId();
addressId = address.getId();
tx.commit();
}
try (Session session = SessionUtil.getSession()) {
department = session.load(Department.class, departmentID);
System.out.println(department.getId());
}
}
当我从数据库加载部门对象时,将执行以下查询。
Hibernate: select department0_.id as id1_2_0_, department0_.name as name2_2_0_ from Department department0_ where department0_.id=?
Hibernate: select address0_.id as id1_0_1_, address0_.city as city2_0_1_, address0_.department_id as departme4_0_1_, address0_.street as street3_0_1_, department1_.id as id1_2_0_, department1_.name as name2_2_0_ from Address address0_ left outer join Department department1_ on address0_.department_id=department1_.id where address0_.department_id=?
1
我很惊讶地看到连接选择查询,因为我从未明确要求地址对象,并且应该延迟加载它。
最佳答案
如果您查看 FetchType.LAZY 的 javadoc,您会发现没有任何保证(与 EAGER fetchinig 相反):
The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified.
对于 OneToOne 可选依赖项(如您的情况)尤其如此。
关于java - Hibernate - FetchType.LAZY 急切地加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42996348/
我有一个案例,在大多数情况下,对象之间的关系是这样的,因此在关系上预先配置一个渴望(加入)的负载是有意义的。但是现在我遇到了一种情况,我真的不想完成急切的加载。 我是否应该从关系中删除连接负载并将所有
在我的 Grails 项目中,我有以下类: class A { static hasMany = [cs:C] } class B { static hasMany = [cs:C]
想象一下以下简化的 DI 模型: @ApplicationScoped public class A { private B b; @Inject public A(B b)
我使用 MapLoader 将数据从数据存储初始加载到 Hazelcast (InitialLoadMode = EAGER)。我需要从一个物化 View 中加载这些数据,该 View 是为了在加载过
我使用 Hibernate Envers 4.3.10.Final。我有以下两个 JPA 类: public class Factory { private int factoryID;
EJB 似乎被延迟加载 - 每当访问时。 但是,我想急切地初始化它们 - 即每当容器启动时。这是如何实现的(尤其是在 JBoss 中) This topic给出了一些提示,但不是很令人满意。 最佳答案
我是一名优秀的程序员,十分优秀!