Spring JPA在repository方法中看不到主外键

n1bvdmb6  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(96)

存储库:

public interface CrimeCaseRepository extends JpaRepository<CrimeCase, Integer> {
    boolean existsByCrimeId(int crimeId);
}

字符串
犯罪案例:

...
    @Id
    @Column(name = "fk_crime")
    private Integer crimeId;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private Crime crime;
...


犯罪:

...
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToOne(mappedBy = "crime", fetch = FetchType.LAZY)
    private CrimeCase crimeCase;
...


DB:

CREATE TABLE crime (
  id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
...
);

CREATE TABLE crime_case (
  fk_crime INTEGER PRIMARY KEY,
...
);


休眠输出:
休眠:从crime_case cc1_0中选择cc1_0.crime_id,其中cc1_0.crime_id=?fetch first?rows only
例外情况:
org.postgresql.util.PSQLException:错误:列cc1_0.crime_id不存在
即使将crimeId重命名为fkCrime也会发生异常。在这种情况下,它必须从Crime实体中获取id

83qze16e

83qze16e1#

如果你使用@MapsId,那么两个实体应该共享相同的主键。也许你不应该开始使用@GeneratedValue(strategy = GenerationType.IDENTITY)
这是给你的reference

已编辑

crime在数据库中是如何Map的?

@JoinColumn(name = "crime_id")
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Crime crime;

字符串

相关问题