Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

java - custom getter method not working in projection in JPA

I have one entity named Employee. I have created one interface (projection) also. In my Employee entity, I have one column named gender which stores M/F in the database. But while fetching the result, I am decoding the values M->Male & F->Female. For this, I have overridden the getGender() method and write code there. But this method not working when I use the projection interface.

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@JsonInclude(value=Include.NON_NULL)
@Table(name = "EMPLOYEE_MASTER")
public class Employee {
    
    @Id
    @Column(name = "EMP_ID")
    private Integer empId;

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

    @Column(name = "GENDER")
    private String gender;

    public void setGender(String gender) {
        this.gender= gender;
    }

    public String getGender() {
        return gender.equals("M")?"Male":"Female";
    }

    .
    .
    .
}

/*Projection*/
public interface EmployeeDetails {
    Integer getEmpId();
    String getName();
    String getGender();
    .
    .
    .
}

/*Repository*/
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer>
    Optional<EmployeeDetails > findByEmpIdIsAndDelDtIsNull(Integer empId);
}

/*Output*/
{
    "empId": "2000001",
    "name": "Mike Thompson",
    "gender": "M",
    .
    .
    .
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...