用join查询ManyToMany关系

时间:2019-04-25 06:52:25

标签: java mysql jdbc spring-jdbc jdbctemplate

请帮助我查询!!! 我有2个与ManyToMany关系的实体。

@Entity
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String headName;

    @ManyToMany(mappedBy = "departmentsList")
    private List<Lector> lectorsList = new ArrayList<>();

public class Lector {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private Degree degree;

    private Integer salary;

    @ManyToMany
    private List<Department> departmentsList = new ArrayList<>();

我想得到一个有名单的Lector部门

public Department findOne(Long id){
        try {
            Department department = (Department) jdbcTemplate.queryForObject(
                    "select * from Department d where d.Id = ? ", new Object[] { id },
                    new BeanPropertyRowMapper(Department.class));

            System.out.println(department);

            return department;


        } catch (EmptyResultDataAccessException notExist) {
            return null;
        }
    }

但是此查询返回了空的“ lectorsList” 我需要使用什么查询?

0 个答案:

没有答案