spring-data-jpa JPA -在非唯一列上使用@Id

dfuffjeb  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(172)

我有一个只读的数据库视图Employee,其中有几列,包括两列codedescriptioncode不是唯一的。例如,

name code description
bob  ft   full time
sue  ft   full time
tom  pt   part time
cat  tm   temporary
mat  ft   full time

但是,由于视图的构建方式,我知道相同的code将始终具有相同的description
我只想得到codedescription的不同列表。
这种方法似乎行得通:

@Entity
@Immutable
@Table("Employee")
public class Code {

  @Id
  private String code;

  private String description;

}

@Repository
public interface CodeRepository extends PagingAndSortingRepository<Code, String> {
} 

@Service
public class CodeService {

  private CodeRepository codeRepo;

  public List<Code> getCodes() {
      return CodeRepository.findAll();
   }
}

由于code不是真正的主键,这种方法是否存在问题?

gkl3eglg

gkl3eglg1#

如果你所做的只是调用这个findAll方法,这是“ok”,但是列表中不包含重复项吗?如果你在这里使用distinct,我想这会更清楚。

相关问题