Reputation: 51
@ManyToOne or @OneToOne will replace the joined column with an entity, but how would I do when I want to use JpaRepository to query by that joined column? There's seems a conflict.
@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);
}
It throws error "property typeId not found", which makes sense because it's surely not in the class "item", instead an entity "type". But how could I query Item by type_id in this situation? And if I declare property typeId, it will throw error duplicated mapping for column "type_id".
Upvotes: 0
Views: 350
Reputation: 27018
You have to do it with _
like
public interface ItemRepository extends JpaRepository<Item, Long> {
List<Item> findByType_Id(Long typeId);
}
Upvotes: 2