简体   繁体   English

在Spring Data JPA中获取非表实体的数据

[英]Fetch data for non table entity in spring data jpa

I have user table with column id, mname, firstname, lastname, age (5 column) using spring data jpa I need to get only id, firstname, age (only 3 column) 我有使用弹簧数据JPA的列ID,MNAME,名字,姓氏,年龄(5列)的用户表,我只需要获取ID,名字,年龄(仅3列)

userentity is below user.java userentity在user.java下面

 @Entity
    @Table(name = "user")
   public class User implements Serializable {

    private static final long serialVersionUID = -3009157732242241606L;
    @Id
    private long id;

    @Column(name = "mname")
    private String mName;

    @Column(name = "firstname")
    private String firstName;

    @Column(name = "firstname")
    private String firstName;

    @Column(name = "lastname")
    private String lastName;

    @Column(name="age")
    private int age;

    //constructor with fileds


user2.java contains the fileds

private Long id;
private String firstname;
private int age;

//getter and setter

UserRepository.java UserRepository.java

public interface UserRepository extends CrudRepository<User, Long> {
@Query(value="SELECT usr.id as id,usr.firstname as firstName, usr.age as age FROM user usr WHERE usr.id=?1", nativeQuery=true)
List<User2> getUserDetailsByUserId(Long id);
}

getting the below error in colsole: 在solele中得到以下错误:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.user.entity.User2] org.springframework.core.convert.ConverterNotFoundException:未找到能够从类型[java.math.BigInteger]转换为类型[com.user.entity.User2]的转换器。

while inspect the code get the error: 在检查代码时出现错误:

Failed to convert from type [java.lang.Object[]] to type [com.user.entity.User2] for value '{403, firstnnnnnnn, 26}'; 无法将值'{403,firstnnnnnnn,26}'的类型[java.lang.Object []]转换为类型[com.user.entity.User2];

Is there any way to map the values to user2 (non-table entity)? 有什么方法可以将值映射到user2(非表实体)?

You should use projections : 您应该使用投影

Interface based : 基于接口

public interface UserProjection {
    Long getId();
    String getFirstname();
    Integer getAge();
}

public interface UserRepository extends CrudRepository<User, Long> {
    List<UserProjection> findById(Long id);
}

Class based (DTO) : 基于类(DTO)

@Value // It's Lombok annotation
public class UserDto {
    Long id;
    String firstname;
    Integer age;
}

public interface UserRepository extends CrudRepository<User, Long> {
    List<UserProjection> findById(Long id);
    List<UserDto> getById(Long id);
}

No, In reality your query return 3 attributes id, firstName, age and not an Object User2 , you can solve your problem like : 不,实际上您的查询返回3个属性id,firstName,age而不是Object User2 ,您可以解决以下问题:

First Solution 第一个解决方案

You can create a constructor in your User2 class which hold this three attributes : 您可以在User2类中创建一个构造函数,该构造函数具有以下三个属性:

public User2(Long id, String firstName, int age){..}

Then your query should look like this : 然后您的查询应如下所示:

SELECT com.packagename.User2(usr.id, usr.firstname, usr.age) FROM user usr WHERE usr.id=?1
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Second Solution 第二解决方案

You have to change : 您必须更改:

List<User2> getUserDetailsByUserId(Long id);
     ^^^^^

to this : 对此:

List<Object[]> getUserDetailsByUserId(Long id);
     ^^^^^^^^

Then you can loop over the result in your service and cast each Object like this : 然后,您可以在服务中遍历结果,并像这样强制转换每个Object:

List<Object[]> listObjects = userRepository.regetUserDetailsByUserId(id);
List<User2> listUsers = new ArrayList<>();
for(Object[] obj : listObjects){
   listUsers.add((Long)obj[0], (String)obj[1], (Integer)obj[2]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM