Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch only selected attributes of an entity using Spring JPA?

Tags:

I'm using Spring Boot (1.3.3.RELEASE) and Hibernate JPA in my project. My entity looks like this:

@Data @NoArgsConstructor @Entity @Table(name = "rule") public class RuleVO {      @Id     @GeneratedValue     private Long id;      @Column(name = "name", length = 128, nullable = false, unique = true)     private String name;      @Column(name = "tag", length = 256)     private String tag;      @OneToMany(mappedBy = "rule", cascade = CascadeType.ALL, orphanRemoval = true)     private List<RuleOutputArticleVO> outputArticles;      @OneToMany(mappedBy = "rule", cascade = CascadeType.ALL, orphanRemoval = true)     private List<RuleInputArticleVO> inputArticles; } 

My repository looks like this:

@Repository public interface RuleRepository extends JpaRepository<RuleVO, Long> { } 

In some cases I need to fetch only id and name attributes of entity RuleVO. How can I achieve this? I found a notice it should be doable using Criteria API and Projections but how? Many thanks in advance. Vojtech

like image 238
Vojtech Avatar asked May 11 '16 15:05

Vojtech


People also ask

What is the method to fetch specific data for a entity from database in Repository class?

Its findById method retrieves an entity by its id.

What is @query annotation in spring boot?

The @Query annotation declares finder queries directly on repository methods. While similar @NamedQuery is used on domain classes, Spring Data JPA @Query annotation is used on Repository interface. This frees the domain classes from persistence specific information, which is a good thing.

What is findById in JPA?

The findById() method is used to retrieves an entity by its id and it is available in CrudRepository interface. The CrudRepository extends Repository interface. In Spring Data JPA Repository is top-level interface in the hierarchy.


1 Answers

UPDATE:

As has been pointed out to me, I'm lazy and this can very well be done hence I'm updating my answer after having looked around the web for a proper one.

Here's an example of how to get only the id's and only the names:

@Repository public interface RuleRepository extends JpaRepository<RuleVO, Long> {      @Query("SELECT r.id FROM RuleVo r where r.name = :name")      List<Long> findIdByName(@Param("name") String name);      @Query("SELECT r.name FROM RuleVo r where r.id = :id")      String findNameById(@Param("id") Long id); } 

Hopefully this update proves helpful


Old Answer:

Only retrieving the specific attributes name/id is not possible as this is not how spring was designed or any SQL database for that matter as you always select a row which is an entity.

What you CAN do is query over the variables in the entity, for instance:

@Repository public interface RuleRepository extends JpaRepository<RuleVO, Long> {      public RuleVo findOneByName(String name);     public RuleVo findOneByNameOrId(String name, Long id);     public List<RuleVo> findAllByName(String name);     // etc, depending on what you want } 

You can modify these however you want w.r.t. your needs. You can call these methods directly via the autowired repository

See http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ Section 5.3 for more options and examples

like image 69
Roel Strolenberg Avatar answered Oct 24 '22 17:10

Roel Strolenberg