![](/img/trans.png)
[英]How to insert data which I get from an List into Database using Entitymanager (Hibernate)?
[英]How to get data of a column into a list from the Database View using Hibernate
我有一個這樣的數據庫視圖:
id sub_id name
1 1 abc
1 2 def
2 1 xyz
2 2 jkl
2 3 lko
映射到 JPA 實體中,如下所示:
@Entity(name = "entity")
@Immutable
class Entity(
@EmbeddedId
val id: Id, // (Combination of id and sub_id)
@Column(name = "name")
val data: List<String>
)
我想從特定 id 的名稱列表中收集不同的名稱到我的 jpa 實體的列表中。 我怎樣才能做到這一點?
謝謝
Hibernate 無法直接實現這一點,但我認為這是Blaze-Persistence Entity Views的完美用例。
我創建了該庫以允許在 JPA 模型和自定義接口或抽象 class 定義的模型之間輕松映射,例如 Spring 類固醇上的數據投影。 這個想法是您以您喜歡的方式定義您的目標結構(域模型),並通過 JPQL 表達式將 map 屬性(吸氣劑)定義為實體 model。
使用 Blaze-Persistence Entity-Views 的 DTO model 可能如下所示:
@EntityView(DatabaseViewEntity.class)
public interface DatabaseViewDto {
@IdMapping("this")
IdDto getId();
@Mapping("name")
List<String> getData();
@EntityView(DatabaseViewEntity.class)
interface IdDto {
Long getId();
Long getSubId();
}
}
使用如下實體類型:
@Entity
public class DatabaseViewEntity {
@Id
private Long id;
@Id
private Long subId;
@Id
private String name;
}
查詢是將實體視圖應用於查詢的問題,最簡單的就是通過 id 進行查詢。
DatabaseViewDto a = entityViewManager.find(entityManager, DatabaseViewDto.class, id);
Spring 數據集成允許您幾乎像 Spring 數據投影一樣使用它: https://persistence.blazebit.com/documentation/entity-view/manual-html/
List<DatabaseViewDto> findAll();
最好的部分是,它只會獲取實際需要的 state!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.