[英]Kryonet desirialization issue with Hibernate Entities
我希望有人以前可能遇到過這個或類似的問題並且可以幫助我:)
我有一個 KryoNet 服務器/客戶端架構,我在其中發送消息。 其中一條消息包含 class“WorldEntity”的實例。 這個實體看起來像這樣:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class WorldEntity {
@OneToMany
private Collection<EntityComponent> components;
}
我正在使用 spring + hibernate 將此實體保存到我的 postgresql 數據庫中。 這一切正常。
當我現在將此實體發送到客戶端時,kryonet 序列化程序會嘗試延遲加載組件,它不應該這樣做,因為客戶端上沒有 hibernate 或數據庫。 事實上,所有數據都已經包含在實體中。
我在某處讀到,可以創建自定義序列化 Object 並將其添加到 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 客戶端的構造函數中用作序列化 object。
任何幫助將不勝感激!
親切的問候達斯汀
我建議您開始考慮為此目的添加 DTO,以避免向您的客戶公開太多信息,以確保安全以及網絡性能問題。 我認為這是Blaze-Persistence Entity Views的完美用例。
我創建了該庫以允許在 JPA 模型和自定義接口或抽象 class 定義的模型之間輕松映射,例如 Spring 類固醇上的數據投影。 這個想法是您以您喜歡的方式定義您的目標結構(域模型),並通過 JPQL 表達式將 map 屬性(吸氣劑)定義為實體 model。
使用 Blaze-Persistence Entity-Views 的 DTO model 可能如下所示:
@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-html/
Page<WorldEntityDto> findAll(Pageable pageable);
最好的部分是,它只會獲取實際需要的 state!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.