我希望有人以前可能遇到过这个或类似的问题,可以帮助我:)
我有一个KryoNet服务器/客户端体系结构,我在其中发送消息。其中一条消息包含类"WorldEntity“的实例。这个实体看起来像这样:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class WorldEntity {
@OneToMany
private Collection<EntityComponent> components;
}我正在使用spring + hibernate将这个实体保存到我的postgresql数据库中。这一切都很好。
当我现在将这个实体发送到客户端时,kryonet序列化器尝试延迟加载组件,这是它不应该做的,因为客户端上没有hibernate或数据库。事实上,所有数据都已经包含在实体中。
我在某处读到,可以创建一个自定义序列化对象并将其添加到KryoNet客户端,以禁用这种hibernate加载:
Kryo kryoSerializer = new Kryo() {
@Override
public Serializer<?> getDefaultSerializer(final Class type) {
if (AbstractPersistentCollection.class.isAssignableFrom(type)) {
return new FieldSerializer(kryoSerializer, type);
}
return super.getDefaultSerializer(type);
}
};不幸的是,这不能用作Kryo客户端的构造函数中的序列化对象。
任何帮助都将不胜感激!
Kind向Dustin致敬
发布于 2021-05-06 02:46:17
我建议您开始考虑为此目的添加DTO,以避免出于安全考虑而将过多信息暴露给您的客户端,同时也避免网络性能方面的问题。我认为这是Blaze-Persistence Entity Views的一个完美用例。
我创建了这个库,以便在JPA模型和自定义接口或抽象类定义的模型之间轻松映射,就像Spring数据在类固醇上的投影一样。其思想是以您喜欢的方式定义目标结构(域模型),并通过JPQL表达式将属性(Getter)映射到实体模型。
使用Blaze-Persistence实体视图,您的用例的DTO模型可能如下所示:
@EntityView(WorldEntity.class)
public interface WorldEntityDto {
@IdMapping
Long getId();
String getName();
Set<EntityComponentDto> getComponents();
@EntityView(EntityComponent.class)
interface EntityComponentDto {
@IdMapping
Long getId();
String getName();
}
}查询就是将实体视图应用于查询,最简单的就是通过id进行查询。
WorldEntityDto a = entityViewManager.find(entityManager, WorldEntityDto.class, id);
Spring数据集成允许您像使用Spring数据投影一样使用它:https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
Page<WorldEntityDto> findAll(Pageable pageable);最好的部分是,它将只获取实际需要的状态!
https://stackoverflow.com/questions/67397834
复制相似问题