JPA-从一对一关系中获取数据会得到null

时间:2019-06-28 16:57:54

标签: java xml spring hibernate jpa

当我保存与WeeklyOccupancy具有一对一关系的关系实体时,一切正常,但是当我尝试取回数据时,相关实体始终为空

我尝试通过JpaRepository方法和本机查询获取数据

在xml中配置了连接列的一种定向的一对一关系

            <one-to-one name="weeklyOccupancy" fetch="EAGER">
                <join-column name="W_OCCUPANCY_ID"/>
                <cascade>
                    <cascade-all/>
                </cascade>
            </one-to-one>

数据表

CREATE TABLE RELATION (
  ID NUMBER(18) NOT NULL PRIMARY KEY,
  ...
  W_OCCUPANCY_ID NUMBER(18)
);

CREATE TABLE WEEKLY_OCCUPANCY (
    ID NUMBER(18) NOT NULL PRIMARY KEY,
    ...
);

ALTER TABLE RELATION
    ADD CONSTRAINT RELATION_W_OCCUPANCY_FK
        FOREIGN KEY (W_OCCUPANCY_ID) REFERENCES WEEKLY_OCCUPANCY (ID);

数据存储在数据库中,但是当我运行

relationRepository.findAll()

使用JpaRepository得到关系,但是应该在里面的weeklyOccupancy始终为空。

我也尝试过使用nativeQuery获取数据:

 @Query(value = "select * from RELATION relation "
            + "LEFT JOIN WEEKLY_OCCUPANCY weeklyOccupancy ON relation.ID = 
             weeklyOccupancy.ID "
            + "WHERE ...", nativeQuery = true)

我启用了spring.jpa.show-sql = true,我看到选择仅适用于关系对象,没有任何联接

Hibernate: 
    select
        relation0_.ID as ID1_0_,

        relation0_.W_OCCUPANCY_ID as W_OCCUPANCY_ID17_0_ from
        ...
        RELATION relation0_

我真的没有主意,任何帮助将不胜感激

0 个答案:

没有答案