LazyInitializationException:跨多个EJB的事务

时间:2018-09-26 05:51:09

标签: java hibernate jpa java-ee ejb

我从EmployeeService中收到以下错误:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

这是我的代码:

GenericService

@Stateless(mappedName = "GenericService")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class GenericService {

    @PersistenceContext(name = "MyPersistenceUnit")
    private EntityManager entityManager;

    public <T> T find(Class<T> clazz, Long oid) {
        return entityManager.find(clazz, oid);
    }
}

EmployeeService

@Stateless(mappedName = "EmployeeService")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class EmployeeService {

    @PersistenceContext(name = "MyPersistenceUnit")
    private EntityManager entityManager;

    @EJB(mappedName = "GenericService")
    private GenericService genericService;

    public void addEmployeeToDepartment(EmployeeBean employeeBean, Long departmentOid) {
        Department department = genericService.find(Department.class, departmentOid);
        // following line is raising the error org.hibernate.LazyInitializationException: could not initialize proxy - no Session
        Set<Employee> employeeList = department.getEmployeeList(); 
        Employee employee = convertToEmployee(employeeBean);
        employeeList.add(employee);
        entityManager.merge(department); 
    }
}

部门

@Entity
@Table(name = "DEPARTMENT")
public class Department implements Serializable {

    private static final long serialVersionUID = -2062922691486346594L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "OID", nullable = false, updatable = false)
    private Long oid;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "department", fetch = FetchType.LAZY, targetEntity = Employee.class)
    private Set<Employee> employeeList;

    public Long getOid() {
        return this.oid;
    }

    public void setOid(Long oid) {
        this.oid = oid;
    }

    public Set<Employee> getEmployeeList() {
        return this.employeeList;
    }

    public void setEmployeeList(Set<Employee> employeeList) {
        this.employeeList = employeeList;
    }
}

员工

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

    private static final long serialVersionUID = 5328841678323494720L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "OID", nullable = false, updatable = false)
    private Long oid;

    @ManyToOne(optional = false)
    @JoinColumn(name = "DEPARTMENT_OID", referencedColumnName = "OID", nullable = false,  updatable = false)    
    private Department department;

    public Long getOid() {
        return this.oid;
    }

    public void setOid(Long oid) {
        this.oid = oid;
    }

    public Department getDepartment() {
        return this.department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">
    <persistence-unit name="MyPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>MyDataSource</jta-data-source>
        <jar-file>datamodel-${project.version}.jar</jar-file>

        <properties>
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="javax.persistence.validation.mode" value="NONE" />
            <property name="tomee.jpa.factory.lazy" value="true" />
        </properties>
    </persistence-unit>
</persistence>
  • TomEE :7.0.4
  • 休眠:5.2.6

0 个答案:

没有答案