@ManyToOne与JpaRepository冲突

时间:2017-12-25 08:55:40

标签: jpa spring-data-jpa

@ManyToOne或@OneToOne将使用实体替换已连接的列,但是当我想使用JpaRepository通过该已连接列进行查询时,我该怎么办?似乎存在冲突。

@Entity
public class Type {
    private Long id;
}

@Entity
public class Item {
    @ManyToOne
    @JoinColumn(name = "type_id")
    private Type type;
}

public interface ItemRepository extends JpaRepository<Item, Long> {
    List<Item> findByTypeId(Long typeId);
}

它抛出错误“property typeId not found”,这是有道理的,因为它肯定不在类“item”中,而是实体“type”。但是在这种情况下我如何通过type_id查询Item?如果我声明属性typeId,它将为列“type_id”抛出错误重复映射。

1 个答案:

答案 0 :(得分:1)

你必须使用_之类的

public interface ItemRepository extends JpaRepository<Item, Long> {
    List<Item> findByType_Id(Long typeId);
}
相关问题