我试图从我的 Employee 类中的用户类中获取“名称”字段,这样我就可以避免进行额外的查询来获取用户名。
目前,我只能通过这种方式获取 user_id
我的用户类
@Entity @Table(name="user", schema = "public") @Getter @Setter @NoArgsConstructor @Accessors(chain = true)
public class User implements Serializable{
private @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "user_id") Long id;
private @Column(unique = true) String email;
private String name;
}
我的员工班
@Entity @Table(name = "employee",schema = "public") @Getter @Setter @NoArgsConstructor @Accessors(chain = true)
public class Employee implements Serializable {
private @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "employee_id") Long id;
@JsonIgnore @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "fk_user_id",insertable = false,updatable = false) private User user;
private @Column(name="fk_user_id") Long userId;
}
我知道不选择名称就无法检索名称,因此我尝试在 crudRepository 上使用自定义查询
public interface EmployeeRepository extends CrudRepository<Employee, Long>{
@Query(value = "SELECT employee.*, public.user.name FROM public.employee INNER JOIN public.user ON public.user.user_id = employee.fk_user_id ", nativeQuery = true)
List<Employee> findEmployees();
}
查询返回我期望的内容,但我不知道如何在我的 Employee 类中获取结果列“name”
欢迎任何帮助或指导,感谢阅读
当年话下
侃侃尔雅
相关分类