具有现有 table spring 数据 jpa 的实体
Entity with existing table spring data jpa
我现有的 table 有 6 列。我可以创建带有自定义列的实体(只有 2 个)吗?我想以只读模式使用这个实体。
table:
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();
}
我现有的 table 有 6 列。我可以创建带有自定义列的实体(只有 2 个)吗?我想以只读模式使用这个实体。
table:
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();
}