我定义了我的模型类,如下所示。
@Entity
@Table(name = "my_employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "emp_address_mapping", joinColumns = @JoinColumn(name = "emp_id"), inverseJoinColumns = @JoinColumn(name = "address_id"))
private List<Address> addresses = new ArrayList<Address>();
.......
.......
}
@Entity
@Table(name = "my_address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String country;
.....
.....
}
public class EmployeeDetails {
private int empId;
private String name;
private String country;
......
......
}
如何使用@Query注释编写查询以填充所有EmployeeDetails。
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
@Query("SELECT new com.sample.app.model.EmployeeDetails......")
List<EmployeeDetails> getEmployeeDetails();
}
答案 0 :(得分:1)
在EmployeeDetails中创建构造函数
public EmployeeDetails(int id,String name,String country){
this.id=id;
this.name=name;
this.country=country;
}
尝试此查询
要获取所有员工详细信息:
SELECT new com.sample.app.model.EmployeeDetails(e.id,e.name,a.country) from Employee e,Address a