Tin Megali
Tin Megali

Reputation: 801

Using Spring JPA Projection with JHipster

I'm having some difficulties to use Spring Data JPA Projections in a project generated with JHipster version 4.14.5.

I'm following Spring's orientation on how to make projections using JPA respositories, however I'm not having any success. The repo give me null values when I try to use projections.

Since I'm not a big connoisseur of JHipster's pipes, I hope somebody out there may help me out. My Entity

@Entity
@Table(name = "research")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Research implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(name = "answer", nullable = false)
private String answer;

@OneToOne(optional = false)
@NotNull
@JoinColumn()
private Question question;
// getters and setters

Entity projection:

public interface ResearchSimple {
    Long getId();
    String getAnswer();
}

Entity's repo:

@Repository
public interface ResearchRepository
extends JpaRepository<Research, Long> {
    @Query("SELECT r FROM Research r)
    List<ResearchSimple> findAllAsSimple();
}

Test Results

List<ResearchSimple> result = repo.findAllAsSimple();
assertEquals(result.size, dbSize); // OK
ResearchSimple simple = result.get(0);
assertNotNull(simple); // OK
assertNotNull(simple.getId); // FAIL!
assertNotNull(simple.getAnswer); // FAIL!

Debugging the value obtained in simple I've noticed that the projection was made, but I can't access it's value. Notice that the class br.com.pixinside.projection.ResearchSimple is present in simple's advised.

org.springframework.aop.framework.ProxyFactory: 2 interfaces    [br.com.pixinside.projection.ResearchSimple, org.springframework.data.projection.TargetAware]; 3 advisors [org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [Pointcut.TRUE]; advice [org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor@1d62a0b], org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [Pointcut.TRUE]; advice [org.springframework.data.projection.ProxyProjectionFactory$TargetAwareMethodInterceptor@3b89f41a], org.springframework.aop.support.DefaultPointcutAdvisor: pointcut [Pointcut.TRUE]; advice [org.springframework.data.projection.ProjectingMethodInterceptor@198453c9]]; targetSource [SingletonTargetSource for target object [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@319267ad]]; proxyTargetClass=false; optimize=false; opaque=true; exposeProxy=false; frozen=false

Upvotes: 0

Views: 365

Answers (1)

Alexander Polozov
Alexander Polozov

Reputation: 433

if you wanna use projection with manually queries , then you should use aliases that matches field names in your projection interface. @Query("SELECT r.id as id, r.answer as answer FROM Research r) Or just skip @Query, and use List<ResearchSimple> findAllSimplifiedBy();

Upvotes: 1

Related Questions