我有一个简单的POJO:
我正在尝试将标题列映射到标题字段:
@SqlResultSetMapping(
name = "ownerSaleMapping",
classes = {
@ConstructorResult(
targetClass = OwnerSale.class,
columns = {
@ColumnResult(name = "title", type = String.class)
}
)
}
)
@NamedNativeQuery(name = "ownerSaleReport", query = "SELECT title FROM content where id=:contentId", resultSetMapping = "ownerSaleMapping")
public class OwnerSale {
public OwnerSale() {
}
private String title;
public OwnerSale(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}我的仓库界面:
public interface OwnerSaleRepository extends JpaRepository<OwnerSale, Long> {
List<OwnerSale> ownerSaleReport(@Param("contentId") Long ownerId);
}但是当我使用它的时候:
List<OwnerSale> sales = ownerSaleRepository.ownerSaleReport(6);我得到了这个错误:
IllegalArgumentException: Not an managed type: class domain.owner.OwnerSale但我需要做一个POJO而不是一个Entity。
解决方案是什么?
发布于 2016-08-31 16:38:54
如果你不能把你的对象转换成Entity,那么也许用简单的RowMapper来使用JdbcTemplate会更容易?
private JdbcTemplate jdbcTemplate; //it depends on your config how to get it here.
List<OwnerSale> ownerSaleReport(Long ownerId){
String sql = "SELECT title FROM content where id=:contentId"
return jdbcTemplate.query(StringUtils.replaceEach(sql,
new String[]{":contentId"},
new String[]{ownerId.toString()}),
(rs, i) -> {
OwnerSale ownerSale = new OwnerSale();
ownerSale.setTitle(rs.getString("title"));//(or using constructor)
return ownerSale;
});
}在这里,我使用的是Apache中的StringUtils,但是您可以使用任何其他方法将args传递给String。
https://stackoverflow.com/questions/39244110
复制相似问题