![](/img/trans.png)
[英]Dropwizard-Hibernate - IllegalArgumentException: No query defined for that name
[英]Dropwizard-Hibernate Lazy fetch is not lazy
我正在将Dropwizard-1.1.2与hibernate-5.2.8一起使用。 我实现了这样的一对多关系:
父表:
@TypeDefs( {@TypeDef( name= "StringJsonObject", typeClass = StringJsonUserType.class)})
@Table(name="parent_table")
@Entity
public class ParentTable {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@Column(name = "parent_id", updatable = false, nullable = false)
private UUID id;
@JoinColumn(name="parent_id")
@OneToMany(targetEntity = NotificationModel.class, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private List<NotificationModel> notifications = new ArrayList<>();
public UUID getId() {
return id;
}
public List<NotificationModel> getNotifications() {
return notifications;
}
public void setNotifications(List<NotificationModel> notifications) {
this.notifications = notifications;
}
}
通知表
@Table(name="notifications")
@Entity
public class ReminderNotificationModel {
@Id
@GeneratedValue
private Integer id;
@Column(name = "notification_id")
@Type(type="pg-uuid")
private UUID notificationId;
private String message;
@Column(name = "notification_status")
private String notificationStatus;
@Column(name = "scheduled_at")
private DateTime scheduledAt;
// getters and constructors
}
现在在我的DAO中,无论我尝试使用本机查询,条件查询还是通过父表获取ID,它始终会同时向我提供所有通知。 通知是否应该延迟获取?
DAO
public class ReminderDao extends AbstractDAO<ReminderModel> {
@Inject
public ReminderDao(SessionFactory sessionFactory) {
super(sessionFactory);
}
public ReminderModel getById(UUID id) {
ReminderModel m = get(id); // m has notifications as well
return m;
}
}
我正在阅读hibernate的文档 ,其中说Lazy是基本类型的提示。 但是,集合不是基本类型。 懒惰应该得到尊重。 我想念什么?
LAZY只是在访问属性时获取值的提示。 除非您使用字节码增强功能,否则Hibernate对于基本类型将忽略此设置。
“懒惰地加载父母的通知”与“不加载父母的通知并假装父母根本没有通知”是不同的。
延迟加载意味着仅在代码从列表中读取通知时(例如,在使用调试器进行打印时,将其序列化为JSON或获取列表的大小时)才加载通知。
如果您真的想测试延迟加载是否正常工作,请加载父级,结束事务并关闭实体管理器,并检查获取列表的大小是否引发异常。 或加载父项,然后分离它,然后检查获取列表的大小是否引发异常。 或加载父对象,然后检查Hibernate.isInitialized(parent.getNotifications())
是否为false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.