我在雇主和雇员之间有一对多的双向关系。
在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
?