首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用Spring数据JPA和Hibernate延迟加载异常

使用Spring数据JPA和Hibernate延迟加载异常
EN

Stack Overflow用户
提问于 2016-01-14 13:01:37
回答 1查看 643关注 0票数 0

我正在尝试学习使用Hibernate的Spring,目前在一个独立项目中(不过稍后我将在Web中使用),而且我得到了例外:

org.hibernate.LazyInitializationException:未能延迟初始化角色集合: com.dbprototype.domain.Crash.vehicles

我不知道为什么。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Entity(name = "STAGING.TEST_CRASH")
    public class Crash {

    @Id
    @Column(name = "C_TEST_CRASH_NUMBER")
    private int crashNum;

    @Column(name = "C_RECORD_NUMBER")
    private int recordNum;

    @Column(name = "C_TOTAL_UNITS")
    private int totalUnits;

    @Column(name = "C_TOTAL_INJURIES")
    private int totalInjured;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="crash")  // fails.
    private List<Vehicle> vehicles; 

    @OneToOne(mappedBy="crash", cascade=CascadeType.ALL)   // this works ok.
    private Metadata metaData;

    public void printCrash() {
        // prints Crash fields, vehicles, and metadata.
    }

    // getters & setters
}


@Entity(name = "STAGING.TEST_VEHICLE")
public class Vehicle {
    @Id
    @Column(name = "V_TEST_VEHICLE_NUMBER")
    private int vehicleNum;

    //@Id
    @Column(name = "C_TEST_CRASH_NUMBER")
    private int crashNum;

    @Column(name = "C_RECORD_NUMBER")
    private int recordNum;

    @Column(name = "V_VEH_MAKE")
    private String vehicleMake;

    @Column(name = "V_VEH_MODEL")
    private String vehicleModel;

    @Column(name = "V_VEH_YEAR")
    private int year;

    @ManyToOne(cascade=CascadeType.ALL)
    private Crash crash;

    // getters & setters
}

我的服务层是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Service
@Transactional(propagation=Propagation.REQUIRED)
public class CrashService {

private CrashDao crashDao;

public void setCrashDao(CrashDao dao) {
    crashDao = dao;
}

public void print() {
    System.out.println("you called CrashService!");
}

// DAO layer will need all 4 CRUD operations on each table.

// read one Crash
public Crash readCrash(int id) {
    return crashDao.findOne(1000);
}

我的DAO层就是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Repository
public interface CrashDao extends CrudRepository<Crash, Integer>{ }

我的spring.xml是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
    ">

<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans -->
<context:component-scan base-package="com.dbprototype" />

<!-- Activates various annotations to be detected in bean classes e.g: @Autowired -->
<context:annotation-config />

<jpa:repositories base-package="com.dbprototype.dao" />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- data source properties -->
</bean>

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="generateDdl" value="false" />
    <property name="showSql" value="true" />
    <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
</bean> 

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />    
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <!-- Spring based scanning for @Entity classes -->
    <property name="packagesToScan" value="com.dbprototype.domain" />
</bean>  

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

我不理解做@OneToMany的正确方法,尽管从一些教程来看,我拥有的是“正确的”。虽然如果我将@OneToMany更改为使用急切加载而不是默认的惰性,但我得到了另一个例外:

org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取ResultSet;SQL n/a;嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet

由: java.sql.SQLSyntaxErrorException: ORA-00904:“VEHICLES1_”引起。“CRASH_C_TEST_CRASH_NUMBER”:无效标识符

有人能帮我理解一下发生了什么,以及如何正确地做OneToMany吗?

谢谢克里斯

EN

回答 1

Stack Overflow用户

发布于 2016-01-14 13:07:11

这种关系是以另一种方式起作用吗?如果您想从任何Crash访问Vechicle

Vechicle表中的联接列是什么?我认为,您忽略了类@JoinColumn中的Vechicle注释。

参见关于WikiBook ManyToOnes:Persistence/ManyToOne的JPA

代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "join_column_in_table_of_vechicle", reverseJoinColumn="C_TEST_CRASH_NUMBER")
private Crash crash;

如果没有,请提供表结构,打开SQL-日志记录,并提供select,这将导致异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34799654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文