使用Spring数据jpa 1.9.1时@NamedEntityGraph不起作用

时间:2015-12-31 11:36:48

标签: spring-data-jpa jpa-2.1 hibernate-entitymanager entitygraph

我定义了以下实体

@Entity
@NamedEntityGraph(name="Course.findByCourseStatusGraph", 
    attributeNodes={
        @NamedAttributeNode("id"),
        @NamedAttributeNode("courseGUID"),
        @NamedAttributeNode(value="courseTitle"),
        @NamedAttributeNode("courseStatus"),
        @NamedAttributeNode("name"),
        @NamedAttributeNode("retired"),
        @NamedAttributeNode("courseType"),
        @NamedAttributeNode("bussinesskey"),
        @NamedAttributeNode(value="coursePlayerType",  subgraph="coursePlayerTypeSubGraph"),
        @NamedAttributeNode(value="AICCAssignableUnit", subgraph="aiccAssignableUnitSubGraph")
    },
    subgraphs={
        @NamedSubgraph(name="coursePlayerTypeSubGraph", attributeNodes={
            @NamedAttributeNode("playerVersion")
    }),
        @NamedSubgraph(name="aiccAssignableUnitSubGraph", attributeNodes={
            @NamedAttributeNode("systemID")
    })
})
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="COURSETYPE")
@org.hibernate.annotations.DiscriminatorOptions(force=true)
public class Course implements Serializable {

    @Id
    @javax.persistence.TableGenerator(name = "COURSE_ID", table = "VU360_SEQ", pkColumnName = "TABLE_NAME", valueColumnName = "NEXT_ID", pkColumnValue = "COURSE", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "COURSE_ID")
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @Column(name = "GUID")
    private String courseGUID;

    @Column(name = "NAME")
    private String courseTitle;

    @Column(name = "COURSESTATUS")
    private String courseStatus;
    ...
    @OneToOne(mappedBy="course" , fetch=FetchType.LAZY) 
    private CoursePlayerType coursePlayerType;

    @OneToOne(mappedBy="course" , fetch=FetchType.LAZY)
    private AICCAssignableUnit AICCAssignableUnit ;
    ....
}

这是我的CoursePlayerType实体

@Entity
@Table(name = "COURSE_PLAYERTYPE")
public class CoursePlayerType implements Serializable {

    @Id
    @javax.persistence.TableGenerator(name = "COURSE_PLAYERTYPE_ID", table = "VU360_SEQ", pkColumnName = "TABLE_NAME", valueColumnName = "NEXT_ID", pkColumnValue = "SEQ", allocationSize = 50)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "COURSE_PLAYERTYPE_ID")
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @Column(name="PLAYER_VERSION")
    private String playerVersion;
    ....
    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="COURSE_ID")
    private Course course;
    ...
}

这是我的AICCAssignableUnit实体

@Entity
@Table(name = "AICC_COURSEASIGNABLEUNIT")
public class AICCAssignableUnit implements Serializable{
    @Id
    @javax.persistence.TableGenerator(name = "AICC_COURSEASIGNABLEUNIT_ID", table = "VU360_SEQ", pkColumnName = "TABLE_NAME", valueColumnName = "NEXT_ID", pkColumnValue = "AICC_COURSEASIGNABLEUNIT", allocationSize = 50)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "AICC_COURSEASIGNABLEUNIT_ID")
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="COURSE_ID")
    private Course course;

    @Column(name="SYSTEM_ID")
    private String systemID;
    .....
}

这是我的存储库

public interface CourseRepository extends CrudRepository<Course, Long>, CourseRepositoryCustom {
    ...
    @EntityGraph(value = "Course.findByCourseStatusGraph", type = EntityGraphType.LOAD)
    @Transactional(readOnly = true)
    @QueryHints(value = @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE, value = "1000" ))
    Stream<Course> findByCourseStatus(String courseStatus);
}

现在我按照下面的方式运行我的测试

@Test
public void test() {

    try (Stream<Course> courseStream = courseRepository.findByCourseStatus(courseStatus);) {
        List<Course> courseList = courseStream.parallel().collect(Collectors.toList());
        List<Long> courseIds = courseList
                .stream()
                .map(Course::getId)
                .collect(Collectors.toList());
    }
}

然后hibernate生成以下查询

- HHH000397: Using ASTQueryTranslatorFactory
- Began transaction (1) for test context [DefaultTestContext@11564455 testClass = CourseRepositoryTest, testInstance = com.softech.vu360.lms.repositories.CourseRepositoryTest@173c0722, testMethod = findCoursesByCustomer@CourseRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@12e3cf1c testClass = CourseRepositoryTest, locations = '{}', classes = '{class com.softech.vu360.lms.LmsTestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@70ed03e4]; rollback [true]
Hibernate: select course0_.ID as ID2_45_, course0_.ADDITIONALMATERIALS as ADDITION3_45_, course0_.APPROVALNUMBER as APPROVAL4_45_, course0_.APPROVEDCOURSEHOURS as APPROVED5_45_, course0_.ASSESMENTS as ASSESMEN6_45_, course0_.BRANDING_ID as BRANDIN58_45_, course0_.BUSINESSUNIT_ID as BUSINESS7_45_, course0_.BUSINESSUNIT_NAME as BUSINESS8_45_, course0_.BUSSINESSKEY as BUSSINES9_45_, course0_.CEUS as CEUS10_45_, course0_.CODE as CODE11_45_, course0_.CONTENTOWNER_ID as CONTENT59_45_, course0_.COURSECATEGORY as COURSEC12_45_, course0_.COURSECONFIGURATIONTEMPLATE_ID as COURSEC60_45_, course0_.GUID as GUID13_45_, course0_.COURSEGUIDE as COURSEG14_45_, course0_.COURSEID as COURSEI15_45_, course0_.COURSEMEDIATYPE as COURSEM16_45_, course0_.COURSEPRE_REQ as COURSEP17_45_, course0_.COURSESTATUS as COURSES18_45_, course0_.NAME as NAME19_45_, course0_.COURSETYPE as COURSETY1_45_, course0_.CREDITHOUR as CREDITH20_45_, course0_.CURRENCY as CURRENC21_45_, course0_.DELIVERYMETHOD as DELIVER22_45_, course0_.DELIVERYMETHOD_ID as DELIVER23_45_, course0_.DESCRIPTION as DESCRIP24_45_, course0_.ENABLEDADDITIONALINFORONCOURSEAPPROVAL as ENABLED25_45_, course0_.ENDOFCOURSEINSTRUCTIONS as ENDOFCO26_45_, course0_.FINALEXAMINFO as FINALEX27_45_, course0_.IMAGEOFCOURSE as IMAGEOF28_45_, course0_.INFORMATIONFORLEARNERREGARDING_REGULATOR as INFORMA29_45_, course0_.KEYWORDS as KEYWORD30_45_, course0_.LANGUAGE_ID as LANGUAG61_45_, course0_.LEARNINGOBJECTIVES as LEARNIN31_45_, course0_.MSRP as MSRP32_45_, course0_.PRODUCTPRICE as PRODUCT33_45_, course0_.PUBLISHEDONVU as PUBLISH34_45_, course0_.PUBLISHEDONSTOREFRONT as PUBLISH35_45_, course0_.QUIZINFO as QUIZINF36_45_, course0_.REGULATINGBODY as REGULAT37_45_, course0_.RETIREDTF as RETIRED38_45_, course0_.RoyaltyAmount as Royalty39_45_, course0_.RoyaltyPartner as Royalty40_45_, course0_.RoyaltyType as Royalty41_45_, course0_.STATE_REGREQ as STATE_R42_45_, course0_.SUBJECTMATTEREXPERTINFO as SUBJECT43_45_, course0_.TOPICSCOVERED as TOPICSC44_45_, course0_.TOS as TOS45_45_, course0_.VERSION as VERSION46_45_, course0_.WholeSalePrice as WholeSa47_45_, course0_.LINK as LINK48_45_, course0_.INSTRUCTORCONNECTEMAILADDRESS as INSTRUC49_45_, course0_.INSTRUCTORCONNECTTYPE as INSTRUC50_45_, course0_.INSTRUCTORCONNECTMEETINGID as INSTRUC51_45_, course0_.INSTRUCTORCONNECTMEETINGPASSCODE as INSTRUC52_45_, course0_.COURSEVENDOR as COURSEV53_45_, course0_.REPORTEDSCORMVERSION as REPORTE54_45_, course0_.HOMEWORKASSIGNMENTDUEDATE as HOMEWOR55_45_, course0_.HOMEWORKASSIGNMENTGRADINGMETHOD as HOMEWOR56_45_, course0_.HOMEWORKASSIGNMENTMASTERYSCORE as HOMEWOR57_45_ from COURSE course0_ where course0_.COURSETYPE in ('Course', 'DFC', 'Weblink', 'Webinar Course', 'External Course', 'Self Paced Course', 'Instructor Connect', 'AICC Course', 'Scorm Course', 'Legacy Course', 'Learner Assignment', 'Classroom Course') and course0_.COURSESTATUS=?

为什么选择所有文件?只应包含实体图中设置的字段?

此后,它开始生成如下所示的查询

Hibernate: select aiccassign0_.ID as ID1_5_0_, aiccassign0_.AUPASSWORD as AUPASSWO2_5_0_, aiccassign0_.COMMANDLINE as COMMANDL3_5_0_, aiccassign0_.COREVENDOR as COREVEND4_5_0_, aiccassign0_.COURSE_ID as COURSE_18_5_0_, aiccassign0_.CREATEDDATE as CREATEDD5_5_0_, aiccassign0_.CREATEDUSERID as CREATEDU6_5_0_, aiccassign0_.FILENAME as FILENAME7_5_0_, aiccassign0_.LASTUPDATEDDATE as LASTUPDA8_5_0_, aiccassign0_.LASTUPDATEDUSER as LASTUPDA9_5_0_, aiccassign0_.MASTERYSCORE as MASTERY10_5_0_, aiccassign0_.MAXSCORE as MAXSCOR11_5_0_, aiccassign0_.MAXTIMEALLOWED_MIN as MAXTIME12_5_0_, aiccassign0_.SYSTEM_ID as SYSTEM_13_5_0_, aiccassign0_.SYSTEMVENDOR as SYSTEMV14_5_0_, aiccassign0_.TIMELIMITACTION as TIMELIM15_5_0_, aiccassign0_.TYPE as TYPE16_5_0_, aiccassign0_.WEBLAUNCH as WEBLAUN17_5_0_ from AICC_COURSEASIGNABLEUNIT aiccassign0_ where aiccassign0_.COURSE_ID=?
Hibernate: select courseplay0_.ID as ID1_59_0_, courseplay0_.COURSE_ID as COURSE_I4_59_0_, courseplay0_.DESCRIPTION as DESCRIPT2_59_0_, courseplay0_.PLAYER_VERSION as PLAYER_V3_59_0_ from COURSE_PLAYERTYPE courseplay0_ where courseplay0_.COURSE_ID=?
...

为什么会产生这么多查询?为什么我的entitygraph没有使用存储库方法。我做错了吗?

由于

0 个答案:

没有答案