首页 / JAVA / 休眠与SparkJava不兼容吗?
休眠与SparkJava不兼容吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了休眠与SparkJava不兼容吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3617字,纯文字阅读大概需要6分钟。
内容图文
![休眠与SparkJava不兼容吗?](/upload/InfoBanner/zyjiaocheng/650/937c34c086ac46c2a37b51e2ccdeee3a.jpg)
在延迟加载模式下将Hibernate与SparkJava一起使用时出现错误.
它在没有SparkJava的情况下可以正常运行,但是在使用SparkJava时,它试图强制为OneToMany关系强制加载.
-型号
@Entity
@Table(name = "KU_SUPPLIER")
public class Supplier {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotEmpty(message = "Please provide a name")
private String name;
@OneToMany(mappedBy = "supplier")
private List<Item> items; // Should be lazy-loaded
// Constructor / Getters / Setters
}
-道
public class SupplierDao implements Dao<Supplier> {
private final SessionFactory sessionFactory;
public SupplierDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
@SuppressWarnings("unchecked")
public List<Supplier> findAll() {
try (Session session = sessionFactory.openSession()) {
return session.createQuery("FROM com.seafrigousa.model.Supplier").getResultList();
}
}
}
-主要
// Working perfectly and lazy-load Items as desired
supplierDao.findAll();
// The method will be called when a web browser goes to "localhost/suppliers"
// It throws org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: model.Supplier.items, could not initialize proxy - no Session
get("/suppliers", "application/json", supplierDao::findAll);
我通过不关闭DAO的会话进行检查,发现Hibernate正在执行查询,就像它处于EAGER加载模式一样,因此它正在执行两个选择,一个用于“供应商”,一个用于“项目”.
有这种现象的原因吗?
谢谢!
解决方法:
我猜这里:get(“ / suppliers”,“ application / json”,providerDao :: findAll);
您正在将Supplier对象序列化为json. Items字段未标记为从序列化中排除,因此获取其值会导致会话之外的延迟初始化(如果会话未关闭,则冗余第二项查询).
如果我的猜测是正确的,请让序列化程序忽略项目字段或在查询中获取它们
session.createQuery("FROM com.seafrigousa.model.Supplier s join fetch s.items").getResultList();
使用gson作为序列化器,您有以下选择:
> @Expose注释要序列化的字段.
@Entity
@Table(name = "KU_SUPPLIER")
public class Supplier {
@Expose
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Expose
@NotEmpty(message = "Please provide a name")
private String name;
@OneToMany(mappedBy = "supplier")
private List<Item> items; // Should be lazy-loaded
// Constructor / Getters / Setters
}
随着以下gson初始化
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
>具有自定义注释的排除策略f.e.
public class IgnoreFieldExclusionStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
return fieldAttributes.getAnnotation(GsonIgnore.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> aClass) {
return false;
}
}
带有自定义批注@GsonIgnore
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface GsonIgnore {}
和gson启动
Gson gson = new GsonBuilder().addSerializationExclusionStrategy(new IgnoreFieldExclusionStrategy()).create();
你的班级看起来像这样
@Entity
@Table(name = "KU_SUPPLIER")
public class Supplier {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotEmpty(message = "Please provide a name")
private String name;
@GsonIgnore
@OneToMany(mappedBy = "supplier")
private List<Item> items; // Should be lazy-loaded
// Constructor / Getters / Setters
}
如果需要使用不同api中的项目序列化Supplier,则可以为Supplier创建DTO对象,并根据如下结果映射它:
package com.seafrigousa.dto
public class SupplierDTO {
private int id;
private String name;
public SupplierDTO(int id, String name) {
this.id = id;
this.name = name;
}
// Getters / Setters
}
并查询:
session.createQuery("select new com.seafrigousa.dto.SupplierDTO(s.id, s.name) FROM com.seafrigousa.model.Supplier s").getResultList();
内容总结
以上是互联网集市为您收集整理的休眠与SparkJava不兼容吗?全部内容,希望文章能够帮你解决休眠与SparkJava不兼容吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。