5

hibernate @version 和 ManyToOne 映射之间有什么关系。

假设我有两张表 Department 和 Employee。这里是 Deparment 是主表,而 Employee 在明细表中。在 Employee 表中,departmentID 作为外键引用。

这是我的课

Public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long ID;
    @Version
    private Long version;

    //Getters and Setters

}

public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long ID;
    @Version
    private Long version;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "departmentID" )
    private Department department;

}

而且,Spring 处理会话。因此,假设在一页中,特定部门被获取并存储在 HTTP 会话中。

现在在另一个页面中,我正在尝试执行以下操作

Employee emp = new Employee();
emp.setName('Test')
emp.setDepartment(dept) // already stored in the HTTP session variable
service.save(emp)

现在我收到以下异常

org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: 

只需按以下方式进行一项更改,就会出现错误

Employee emp = new Employee();
emp.setName('Test')
dept.setVersion(0);
emp.setDepartment(dept) // already stored in the HTTP session variable
service.save(emp)

我的春天配置

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

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- Container Configuration: The IOC container configuration xml file is 
    shown below,The container has the <context:component-scan> element and <context:annotation-config/> 
    <context:annotation-config/> used to intimate the beans of this IOC container 
    are annotation supported. By pass the base path of the beans as the value 
    of the base-package attribute of context:component-scan element, we can detect 
    the beans and registering their bean definitions automatically without lots 
    of overhead. The value of base-package attribute is fully qualified package 
    name of the bean classes. We can pass more than one package names by comma 
    separated -->

<context:annotation-config />
<context:component-scan base-package="com.product.business" />

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

<!-- This will ensure that hibernate or jpa exceptions are automatically 
    translated into Spring's generic DataAccessException hierarchy for those 
    classes annotated with Repository -->

<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="CRUDService" class="com.product.business.service.CRUDServiceImpl" />
<bean id="AuthService" class="com.product.business.service.AuthServiceImpl" />

服务实施

package com.product.business.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.product.business.dao.CRUDDao;

@Service
public class CRUDServiceImpl implements CRUDService {

    @Autowired
    private CRUDDao CRUDDao;

    @Transactional(readOnly = true)
    public <T> List<T> getAll(Class<T> klass) {
        return CRUDDao.getAll(klass);
    }

    @Transactional
    public <T> void Save(T klass) throws DataAccessException {
        CRUDDao.Save(klass);
    }

    @Transactional
    public <T> void delete(T klass) throws DataAccessException {
        CRUDDao.delete(klass);
    }

    @Transactional
    public <T> T GetUniqueEntityByNamedQuery(String query, Object... params) {
        return CRUDDao.GetUniqueEntityByNamedQuery(query, params);
    }

    @Transactional
    public <T> List<T> GetListByNamedQuery(String query, Object... params) {
        return CRUDDao.GetListByNamedQuery(query, params);
    }

    @Override
    @Transactional(readOnly = true)
    public <T> Long getQueryCount(String query, Object... params) {
        return CRUDDao.getQueryCount(query, params);
    }

    @Override
    @Transactional(readOnly = true)
    public <T> T findByPrimaryKey(Class<T> klass, Serializable id) {
         return CRUDDao.findByPrimaryKey(klass, id);
    }

}
4

3 回答 3

4

您需要先保存,Department然后再保存Employee.

service.save(dept);
service.save(emp);

更新以回应您的评论:

为了将员工与部门相关联,您需要有一个存在的部门。请记住,在您的数据库中,Employee 对 Department 有一个 FK,所以 Hibernate 抱怨的是您试图用一个不存在的 Department 保存一个 Employee,所以您有以下选项:

  1. 如果部门是一个新部门,您必须先保存它,然后再保存员工。
  2. 通过 entityManager.find(id, Department.class) 之类的查询查找已存储的部门,并在您的 Employee 对象中使用该对象。
  3. 将您与员工部门的关系标记为@Cascade
于 2013-09-24T13:53:41.537 回答
0

您的主要问题不是级联而是获取。

请参阅以下帖子: Java Persistence API 中 FetchType LAZY 和 EAGER 之间的区别?

@ManyToOne(fetch = FetchType.EAGER)

否则,正如您所提到的,您的会话将关闭并且您丢失了对象。渴望获取将确保它保持打开状态

于 2013-12-07T14:40:18.447 回答
0

我知道这是一个旧帖子,但也许这会对其他人有所帮助。

这假设您使用 Hibernate 作为 JPA 实现。

由于您的部门存储在会话中,因此可以肯定地说它是分离的。

由于您没有修改 Department 实例,因此这种情况的最佳途径是:

Employee emp = new Employee();
emp.setName("Test");
emp.setDepartment(em.getReference(Department.class, dept.getID());
service.save(emp);

请参阅此处的 Hibernate 文档:实体管理器 - 加载对象

如果您收到 EntityNotFoundException,请确保最初检索 Department 的代码在检索它的事务中至少调用了一个方法。

于 2014-12-04T16:57:30.597 回答