Home > Net >  Is there any way to select specific columns and make them entity without creating any interface or D
Is there any way to select specific columns and make them entity without creating any interface or D

Time:11-06

I would like to select only some specific columns from the table. And I want to receive the result as an entity without creating any interface or DTO to projection.

Here's my entity and query

@Getter
@Setter
@Table(name = "member")
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class MemberEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "member_num")
    private Integer memberNum;

    @Column(name = "member_id")
    private String id;

    @Column(name = "member_password")
    private String password;

    @Column(name = "member_name")
    private String name;

    @Column(name = "member_email")
    private String email;

    @Column(name = "member_phone")
    private String phone;

    @Column(name = "member_student_num")
    private String studentNum;

    @Column(name = "member_type")
    private String type;

    @Column(name = "member_refresh_token")
    private String refreshToken;

    @Column(name = "member_dormitory")
    private Integer dormitoryNum;
}
@Query(value = "SELECT new com.project.myProject.member.domain.entity.MemberEntity(,,m.member_password,,,,,,m.member_refresh_token,) FROM myProject.member as m WHERE m.member_id = :memberId", nativeQuery = true)
    MemberEntity findByMemberId(@Param("memberId") String memberId);

When I run this query an error occurs.

2023-11-05T22:55:32.027 09:00 ERROR 58940 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.project.myProject.member.domain.entity.MemberEntity(,,m.member_password,,,,,,m.me' at line 1

org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [SELECT new com.project.myProject.member.domain.entity.MemberEntity(,,m.member_password,,,,,,m.member_refresh_token,) FROM myProject.member as m WHERE m.member_id = ?] [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.project.myProject.member.domain.entity.MemberEntity(,,m.member_password,,,,,,m.me' at line 1] [n/a]; SQL [n/a]

Here's what I've tried

  1. Replace @NoArgsConstructor, @AllArgsConstructor to @RequiredArgsConstructor in MemberEntity Class
  2. change "new com.project.myProject.member.domain.entity.MemberEntity(, , m.member_password, , , , , , m.member_refresh_token, )" to "new com.project.myProject.member.domain.entity.MemberEntity(m.member_password, m.member_refresh_token)" in query

CodePudding user response:

Based on your information that you provide I can suggest you to do something like next one approach.

In MemberEntity create a Constructor that match your requirements about fields.

public class MemberEntity {
     
     //fields
       ...
      public MemberEntity(String password, String refreshToken) {
        this.password = password;
        this.refreshToken = refreshToken;
      }
}

To know if the query is a native one it cannot use framework features as(DTO Projection) or anything else and based on it the query now should be as next one.

  @Query(value = "SELECT new com.project.myProject.member.domain.entity.MemberEntity(m.password, m.refreshToken) FROM MemberEntity m WHERE m.id = :memberId ")
  MemberEntity findByMemberId(@Param("memberId") String memberId);

And I should fix your current problem.

  • Related