【发布时间】:2018-09-05 02:53:04
【问题描述】:
我正在使用 Spring JPA,我需要一个本机查询。通过该查询,我只需要从表中获取两个字段,因此我尝试使用Projections。它不起作用,这是我得到的错误:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.example.IdsOnly]
我试图准确地遵循我链接的那个页面的说明,我试图让我的查询非本地(如果我使用投影,我真的需要它是本地的吗,顺便说一句?),但我总是得到那个错误。
如果我使用一个接口它可以工作,但结果是代理,我真的需要它们是“正常结果”,我可以变成 json。
所以,这是我的代码。实体:
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@Entity
@Table(name = "TestTable")
public class TestTable {
@Id
@Basic(optional = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "Id")
private Integer id;
@Column(name = "OtherId")
private String otherId;
@Column(name = "CreationDate")
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@Column(name = "Type")
private Integer type;
}
投影的类:
import lombok.Value;
@Value // This annotation fills in the "hashCode" and "equals" methods, plus the all-arguments constructor
public class IdsOnly {
private final Integer id;
private final String otherId;
}
存储库:
public interface TestTableRepository extends JpaRepository<TestTable, Integer> {
@Query(value = "select Id, OtherId from TestTable where CreationDate > ?1 and Type in (?2)", nativeQuery = true)
public Collection<IdsOnly> findEntriesAfterDate(Date creationDate, List<Integer> types);
}
以及试图获取数据的代码:
@Autowired
TestTableRepository ttRepo;
...
Date theDate = ...
List<Integer> theListOfTypes = ...
...
Collection<IdsOnly> results = ttRepo.findEntriesAfterDate(theDate, theListOfTypes);
感谢您的帮助。我真的不明白我做错了什么。
【问题讨论】:
-
代码中的
MyProjectionClass类在哪里 -
抱歉,我在将实际代码设为“伪”时更改了它的名称。我编辑了问题,使错误显示“com.example.IdsOnly”。
标签: java spring spring-data-jpa projection