gpt4 book ai didi

java - hibernate 多对一急切获取不起作用

转载 作者:行者123 更新时间:2023-12-01 06:13:37 24 4
gpt4 key购买 nike

我正在尝试使用 hibernate 注释从数据库中获取数据,但它没有按我的预期工作。我有两个表“员工”和“部门”。 java代码如下:

员工类别

 @Entity
@Table(name = "EMPLOYEE")
public class EmployeeBO {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "EMPID")
private int empId;
@Column(name = "EMPNAME")
private String empName;
@Column(name = "SALARY")
private int empSalary;
@Column(name = "EMPADDRESS")
private String empAddress;
@Column(name = "EMPAGE")
private int empAge;
@Column(name = "DEPTID")
private Integer deptId;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="DEPTID", nullable = true, insertable = false, updatable = false)
private DepartmentBO departmentBO;
// getter and setter
}

院系类别:

@Entity
@Table(name = "DEPARTMENT")
public class DepartmentBO {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "DEPTID")
private int deptid;

@Column(name = "DEPTNAME")
private String deptName;
// getter and setter.
}

spring-servlet.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--?xml version="1.0" encoding="UTF-8"?-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<context:annotation-config></context:annotation-config>

<context:component-scan base-package="com.web"></context:component-scan>

<context:property-placeholder location="file:D:/EasyProp/db.properties" ignore-unresolvable="false"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="basicDataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${driver.classname}" />
<property name="url" value="${driver.url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- <property name="initialSize" value="10"/> -->
<!-- <property name="maxActive" value="25"/> -->
<!-- <property name="maxIdle" value="25"/> -->
<!-- <aop:scoped-proxy/> -->
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>

<!-- <bean id="dbUtil" class="com.web.utility.OnLoadStartUp" init-method="initialize">
<property name="dataSource" ref="basicDataSource" />
</bean> -->

<!-- <bean id="loadOnStartUp" class="com.web.utility.LoadOnStartUp">
<property name="baseService" ref="baseService"></property>
</bean> -->

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="basicDataSource" />
<property name="annotatedClasses">
<list>
<value>com.web.bo.EmployeeBO</value>
<value>com.web.bo.DepartmentBO</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hmb2ddl.auto">validate</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.jdbc.fetch_size">10</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.default_batch_fetch_size">8</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.connection.release_mode">on_close</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.bytecode.provider">javassist</prop>
</props>
</property>
</bean>
</beans>

当我尝试从员工和部门表中获取所有数据但它只返回员工表中的数据时,我的 hql 查询代码如下:

List<EmployeeBO> empList = null; 
empList = sessionFactory.getCurrentSession().createQuery("From "+employeeBO.getClass().getName()).list();

上面的查询在数据库上的命中如下。

select employeebo0_.EMPID as EMPID0_, employeebo0_.DEPTID as DEPTID0_, employeebo0_.EMPADDRESS as EMPADDRESS0_, employeebo0_.EMPAGE as EMPAGE0_, employeebo0_.EMPNAME as EMPNAME0_, employeebo0_.SALARY as SALARY0_ from EMPLOYEE employeebo0_

但我想要员工表和部门表的所有数据都使用急切获取。

最佳答案

它按预期工作。 EmployeeBO 包含一个DepartmentBO。 DepartmentBO 将在您访问时自动填充。

如果包含 EmployeeBO 的 Hibernate session 已结束,则情况并非如此。那么 EmployeeBO 是暂时的,hibernate 将不会获取丢失的对象。

要强制填充 DepartmentBO,您可以在 hql 代码中添加 fetch 子句。但这不再是懒惰获取了。

HQL

from EmployeeBO join fetch EmployeeBO.department 

应该完成这项工作(将 EmployeeBO 替换为实际的实体名称)。

请参阅 hibernate 文档:https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html以及此处的获取策略 https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html#performance-fetching

从评论来看,您实际上希望对关系进行急切获取,因此您需要将 fetchtype 声明为急切:

@ManyToOne(fetch=FetchType.EAGER)

应该修复它。在此处添加另一个文档:https://docs.oracle.com/javaee/6/api/javax/persistence/ManyToOne.html

关于java - hibernate 多对一急切获取不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29534882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com