[英]Entity with existing table spring data jpa
我有一個現有的 6 列表。 我可以使用自定義列創建實體(僅 2 個)嗎? 我想以只讀模式使用這個實體。
桌子:
create table ES_USER_GROUPS
(
group_id NUMBER(9) not null,
alias VARCHAR2(200) not null,
name_es VARCHAR2(200 CHAR) not null,
name_lat VARCHAR2(200 CHAR),
name_en VARCHAR2(200 CHAR),
state VARCHAR2(1) not null
)
實體:
@Data
@Entity
@Table(name = "es_user_groups")
public class UserGroup {
private Integer groupId;
private String alias;
}
是的你可以。 但是您應該將列設置為只讀。
@Data
@Entity
@Table(name = "es_user_groups")
public class UserGroup {
@Id @Column(insertable=false, updateable=false)
private Integer groupId;
@Column(insertable=false, updateable=false)
private String alias;
}
最干凈的方法是使用投影,即 class 具有您想要獲取並在存儲庫中使用它的字段,不需要額外的映射:
實體:
@Data
public class UserGroupDTO {
private Integer groupId;
private String alias;
}
存儲庫:
@Repository
public interface UserGroupRepository extends Repository<UserGroup, Integer> {
List<UserGroupDTO> findAll();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.