使用 Spring Data JPA 选择一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28912422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:25:22  来源:igfitidea点击:

Select one column using Spring Data JPA

springspring-bootspring-data-jpaspring-data-rest

提问by Alax

Does anyone have any idea how to get a single column using Spring Data JPA? I created a repository like below in my Spring Boot project, but always get the {"cause":null,"message":"PersistentEntity must not be null!"}error when accessing the Restful URL.

有谁知道如何使用 Spring Data JPA 获取单个列?我在 Spring Boot 项目中创建了一个如下所示的存储库,但{"cause":null,"message":"PersistentEntity must not be null!"}在访问 Restful URL 时总是出现错误。

@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UsersRepository extends CrudRepository<Users, Integer> {

    @Query("SELECT u.userName  FROM Users u")
    public List<String> getUserName();
}

Then if I access the Restful URL like ../users/search/getUserName, I get the error: {"cause":null,"message":"PersistentEntity must not be null!"}

然后,如果我访问 Restful URL,例如../users/search/getUserName,我会收到错误消息: {"cause":null,"message":"PersistentEntity must not be null!"}

回答by Abhijeet Behare

This Works for me.

这对我有用。

public interface UserDataRepository extends JpaRepository<UserData, Long> {

    @Query(value = "SELECT emp_name FROM user_data", nativeQuery = true)
    public List<Object[]> findEmp_name();
}


System.out.println("data"+  userDataRepository.findEmp_name());

The above line gave me this result :

上面的行给了我这个结果:

data[abhijeet, abhijeet1, abhijeet2, abhijeet3, abhijeet4, abhijeet5]

数据[abhijeet, abhijeet1, abhijeet2, abhijeet3, abhijeet4, abhijeet5]

回答by Ethiraj

Concept is : In your entity class create a constructor with only required instant variables. And use that constructor in the repository method shown below.

概念是:在您的实体类中创建一个仅包含所需即时变量的构造函数。并在下面显示的存储库方法中使用该构造函数。

Lets say you have a interface Repository like below

假设您有一个如下所示的接口存储库

  1. Repository implementation:

    public interface UserRepository<User> extends JpaRepository<User,String>
    {
        @Query(value = "select new com.org.User(usr.userId) from User usr where usr.name(:name))")
        List<User> findUserIdAlone(@Param("name") String user);
    }
    
  2. In Controller

    @RestController
    public class UserController 
    {
        @Autowired
        private UserRepository<User> userRepository; 
    
        @Res
        public ResponseEntity<User> getUser(@PathVariable("usrname") String userName)
        {
            User resultUser = usrRepository.findUserIdAlone(userName);
            return ResponseEntity.ok(resultUser);
        }
    }
    
    public class User 
    {
    
        private String userId,userName;
    
        public User(String userId) 
        {
            this.userId=userId;
        }
        // setter and getters goes here
    }
    
  1. 存储库实现:

    public interface UserRepository<User> extends JpaRepository<User,String>
    {
        @Query(value = "select new com.org.User(usr.userId) from User usr where usr.name(:name))")
        List<User> findUserIdAlone(@Param("name") String user);
    }
    
  2. 在控制器中

    @RestController
    public class UserController 
    {
        @Autowired
        private UserRepository<User> userRepository; 
    
        @Res
        public ResponseEntity<User> getUser(@PathVariable("usrname") String userName)
        {
            User resultUser = usrRepository.findUserIdAlone(userName);
            return ResponseEntity.ok(resultUser);
        }
    }
    
    public class User 
    {
    
        private String userId,userName;
    
        public User(String userId) 
        {
            this.userId=userId;
        }
        // setter and getters goes here
    }
    

回答by Murder

If you need list all of the users, try select userName from Users, if you need one user use "where"look at spring data JPA http://docs.spring.io/spring-data/jpa/docs/current/reference/html/, try change CrudRepository to JpaRepository

如果您需要列出所有用户,请尝试select userName from Users,如果您需要一个用户使用"where"查看 spring 数据 JPA http://docs.spring.io/spring-data/jpa/docs/current/reference/html/,请尝试更改 CrudRepository到 JpaRepository

回答by Hatem Jaber

If you want to only return a single column you should look at Projections and Excerptswhich will allow you to filter specific columns and other things that are usefule.

如果您只想返回单个列,您应该查看Projections 和 Excerpts,这将允许您过滤特定的列和其他有用的内容。