【问题标题】:JPA - Using @Id on a non-unique columnJPA - 在非唯一列上使用 @Id
【发布时间】:2022-08-22 16:59:07
【问题描述】:

我有一个只读数据库视图,Employee,有几个列,包括两个名为 codedescription 的列。 code 不是唯一的。例如,

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

由于视图的构建方式,我知道相同的code 将始终具有相同的description

我只对获得codes 和descriptions 的不同列表感兴趣。

这种方法似乎有效:

@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 不是真正的主键?

    标签: java hibernate jpa spring-data-jpa sql-view


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-06-20
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多