Hibernate JoinColumn 注释不限制命名查询 where 子句

Hibernate JoinColumn annotation not restricting on named query where clause

我正在玩 hibernate,我已经下载了以下测试数据库 https://dev.mysql.com/doc/employee/en/sakila-structure.html

我有一个关于员工 class 的命名查询:

@NamedNativeQuery(
        name="complexQuery",
        query="select * from employees inner join salaries on employees.emp_no=salaries.emp_no where salaries.from_date < 19870101 " +
                "AND employees.emp_no = 10064;",
        resultClass=Employee.class
)

我已经通过以下方式将员工映射到薪水:

@OneToMany(
        cascade = CascadeType.ALL,
        fetch = FetchType.EAGER
)
@JoinColumn(name = "emp_no", nullable = false, insertable=false, updatable=false)
private Set<Salary> salaries = new HashSet<>();

我预计以下休眠查询将包含

的 where 语句

salaries.from_date < 19870101

但是我注意到实际上休眠查询工资会选择该员工 ID 的所有行:

Hibernate: 
    select
        * 
    from
        employees 
    inner join
        salaries 
            on employees.emp_no=salaries.emp_no 
    where
        salaries.from_date < 19870101 
        AND employees.emp_no = 10064;
Hibernate: 
    select
        salaries0_.emp_no as emp_no1_4_0_,
        salaries0_.from_date as from_dat2_4_0_,
        salaries0_.emp_no as emp_no1_4_1_,
        salaries0_.from_date as from_dat2_4_1_,
        salaries0_.salary as salary3_4_1_,
        salaries0_.to_date as to_date4_4_1_ 
    from
        salaries salaries0_ 
    where
        salaries0_.emp_no=?

有没有自动生成的工资查询也包括

where
            salaries0_.from_date < ? 
            AND salaries0_.emp_no=?

编辑:我也遇到了命名查询的问题:

@NamedQuery(
        name="complexQuery",
        query="select e " +
                "from Employee e, Salary s " +
                "where e.id = 10064 " +
                "AND s.id.fromDate < 19900101" +
                "AND s.id.empNo = 10064"
                )

您没有加入 Employee 和 Salary!

正确的说法

@NamedQuery(
    name="complexQuery",
    query="select e " +
            "from Employee e join e.salaries s " +
            "where e.id = 10064 " +
            "AND s.id.fromDate < 19900101"
            )