Home > Enterprise >  Spring boot Interface Projection only returns null
Spring boot Interface Projection only returns null

Time:11-05

I am using spring data jpa. The goal is to fetch from my entry table only the columns "title" and "creation_date". Apparently this is possible with something called Interface projection. So i created the interface and changed the return type to the interface, the interface gets returned, but when i call the get methods/return it the values are only null.

This is my entity class:

import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.sun.istack.NotNull;

@Entity
public class Entry {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    
    @Column(columnDefinition="timestamptz")
    Instant creationDate;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="userId")
    @NotNull
    private User user;
    
    @Column(columnDefinition = "varchar(256)") 
    String title;
    @Column(columnDefinition = "text") 
    String text;
    
    
    
    
    public Entry() {
        creationDate = Instant.now();
    }




    public long getId() {
        return id;
    }




    public void setId(long id) {
        this.id = id;
    }




    public Instant getCreationDate() {
        return creationDate;
    }




    public void setCreationDate(Instant creationDate) {
        this.creationDate = creationDate;
    }




    public User getUser() {
        return user;
    }




    public void setUser(User user) {
        this.user = user;
    }




    public String getTitle() {
        return title;
    }




    public void setTitle(String title) {
        this.title = title;
    }




    public String getText() {
        return text;
    }




    public void setText(String text) {
        this.text = text;
    }
    
    
    
    

}

This is my interface:

    import java.time.Instant;
    
    public interface EntryTitleDateAndUserIdDTO {
        public String getTitle();
        public Instant getCreationDate();
        //User getUser();
        

}

This is my Repository:

public interface EntryRepository extends JpaRepository<Entry, Long>{
    
    @Query("SELECT e.title, e.creationDate FROM Entry e WHERE e.user = ?1")
    ArrayList<EntryTitleDateAndUserIdDTO> getAllEntriesOfUser(User user);
    
    @Query("SELECT e FROM Entry e WHERE e.id = ?1 AND e.user = ?2")
    Entry getEntryOfUser(Long entryId, Long userId );
    
    @Query("DELETE FROM Entry e WHERE e.id = ?1 AND e.user = ?2")
    Entry deleteEntry(Long entryId, Long userId );

}

This is my controller:

@RestController
@RequestMapping("/User")
public class UserController {
    
    @Autowired
    UserRepository userRepo;
    
    @Autowired
    EntryRepository entryRepo;
    
    
    
    @GetMapping("/{userId}/Entries")
    private ArrayList<EntryTitleDateAndUserIdDTO> getAllEntries(@PathVariable Long userId) {
        
        User u = userRepo.findById(userId).get();
        ArrayList<EntryTitleDateAndUserIdDTO> e = entryRepo.getAllEntriesOfUser(u);
        
        
        
        return e;
        
    }

And this is the output in the browser:

[{"title":null,"creationDate":null}]

And this is whats safed in the db: Postgresql entry table

This is the Query generated by Spring:

    select
        user0_.id as id1_1_0_,
        user0_.email as email2_1_0_,
        user0_.join_date as join_dat3_1_0_,
        user0_.password as password4_1_0_,
        user0_.username as username5_1_0_ 
    from
        public.user user0_ 
    where
        user0_.id=?
2021-11-04 18:13:35.417 TRACE 5104 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]
2021-11-04 18:13:35.447 DEBUG 5104 --- [nio-8080-exec-1] org.hibernate.SQL                        : 
    select
        entry0_.title as col_0_0_,
        entry0_.creation_date as col_1_0_ 
    from
        public.entry entry0_ 
    where
        entry0_.user_id=?
2021-11-04 18:13:35.448 TRACE 5104 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1]

If anyone could help, that would be appreciated, thanks in advance.

CodePudding user response:

Try aliasing your select fields to match exactly your Projection Interface methods getTitle and getCreationDate

 @Query("SELECT e.title as title, e.creationDate as creationDate FROM Entry e WHERE e.user = ?1")
  • Related