@Fetch(FetchMode.JOIN)不适用于org.springframework.orm.jpa.vendor..HibernateJpaVendorAdapter?

时间:2014-06-16 19:01:31

标签: java spring hibernate jpa

我在雇主和雇员之间有一对多的双向关系。 在Employee实体中,我想急切地获取Employer实例。

我的Employee实体是:

@Entity
@Table(name = "EMPLOYEE", schema = "hibernatesimpledb")
public class Employee implements Serializable {
................
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @Fetch(FetchMode.JOIN)
    private Employer employer;
.....................
}

LocalContainerEntityManagerFactoryBean的弹簧配置是:

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.livelessons.spring.springcore.entities" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

问题是hibernate在选择单个Employee实体时会生成两个select语句。一个选项适用于Employee,另一个适用于Employer

为什么@Fetch(FetchMode.JOIN)不适用于HibernateJpaVendorAdapter

0 个答案:

没有答案