【发布时间】:2019-03-01 22:05:08
【问题描述】:
我的域类中有一个映射属性,我正在尝试在存储库中创建一个查询或使用默认的“findByBlah”语法从数据库中提取属性。目前不会工作。我可以轻松地用 SQL 编写查询,但我不知道 JPQL 期望什么。如何使用 JPQL 或接口“findBy”语法从数据库中提取这些数据?无论我当前使用哪种存储库方法,当它从数据库中提取“收集器”时,属性(复杂对象的映射或列表)始终为空。
域对象:
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "collector")
public class Collector {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "collector_id")
private Long id;
@NotNull
private String name;
@ElementCollection
@MapKeyColumn(name = "attribute_name")
@Column(name = "attribute_value")
@CollectionTable(name = "collector_attributes", joinColumns = @JoinColumn(name = "collector_id"))
private Map<String, String> attributes;
@Override
public String toString() {
return ObjectUtil.print(this);
}
}
存储库:
public interface CollectorRepository extends PagingAndSortingRepository<Collector, Long> {
@Query(value = "select c from Collector c where c.attributes[$1] = $2")
Page<Collector> findByAttributesNameAndValue(String name, String value, Pageable pageable);
}
这是在 H2 控制台中工作的查询:
SELECT * FROM Collector a INNER JOIN collector_attributes b ON a.collector_id = b.collector_id where b.attribute_name= 'nickName' and b.attribute_value = 'Critikon'
【问题讨论】:
标签: sql spring-boot spring-data-jpa jpql