使用Hibernate 5,可以使用字节码增强轻松完成此 *** 作。
首先,您需要添加以下Maven插件:
<plugin> <groupId>org.hibernate.orm.tooling</groupId> <artifactId>hibernate-enhance-maven-plugin</artifactId> <version>${hibernate.version}</version> <executions> <execution> <configuration> <enableLazyInitialization>true</enableLazyInitialization> </configuration> <goals> <goal>enhance</goal> </goals> </execution> </executions></plugin>
然后,您可以简单地使用以下注释您的实体属性
@Basic(fetch = FetchType.LAZY):
@Entity(name = "Event")@Table(name = "event")public class Event extends baseEntity { @Type(type = "jsonb") @Column(columnDefinition = "jsonb") @Basic(fetch = FetchType.LAZY) private Location location; public Location getLocation() { return location; } public void setLocation(Location location) { this.location = location; }}
当您获取实体时:
Event event = entityManager.find(Event.class, eventHolder.get().getId());LOGGER.debug("Fetched event");assertEquals("Cluj-Napoca", event.getLocation().getCity());
Hibernate将使用辅助选择来加载惰性属性:
SELECt e.id AS id1_0_0_FROM event eWHERe e.id = 1-- Fetched eventSELECt e.location AS location2_0_FROM event eWHERe e.id = 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)