2

I have a simple POJO:

I'm trying to map title column to title field:

@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;
    }
}

My repository interface:

public interface OwnerSaleRepository extends JpaRepository<OwnerSale, Long> {

    List<OwnerSale> ownerSaleReport(@Param("contentId") Long ownerId);
}

But when i'm using that:

List<OwnerSale> sales = ownerSaleRepository.ownerSaleReport(6);

I got this error:

IllegalArgumentException: Not an managed type: class domain.owner.OwnerSale

But i need to OwnerSale be a POJO not an Entity.

what is the solution?

2
  • actually I'm not sure that you can use not-Entity class here JpaRepository<OwnerSale, Long> Commented Aug 31, 2016 at 8:42
  • AFAIK adding annotation don't force class to become not POJO link. And it's strange that you can add @SqlResultSetMapping and @NamedNativeQuery but can't add @Entity and @Id Commented Aug 31, 2016 at 8:48

1 Answer 1

1

If you can't turn your object to Entity, then maybe it's easier to use JdbcTemplate with simple RowMapper?

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;
               });
}

Here I use StringUtils from Apache but you can use any other way to pass args to String

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.