我正在使用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
类在代码中的哪里? - Jose Da Silva