深入Hibernate,我偶然发现了以下情况:我有一个@Entity Person
@Entity
@Table(name = "PERSON")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="person")
public class Person implements PersonInterface{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
protected Integer id;
@Column(name = "NAME")
protected String name;
@ElementCollection
@Formula("(SELECT groupId FROM PERSON_GROUPS WHERE id = id")
protected List<Integer> groups = new ArrayList<>();
@Override
public Integer getId() {
return id;
}
@Override
public Collection<Integer> getGroups(){
return groups;
}
@Override
public String getName() {
return name;
}
}
它分为两部分,不可变和可变:
@Entity
@DiscriminatorValue("mutableperson")
@Transactional
public class MutablePerson extends Person implements MutablePersonInterface{
@Override
public void setName(String name) {
this.name = name;
}
@Override
public void setId(Integer id) {
this.id = id;
}
@Override
public void setGroups(List<Integer> groupIds) {
this.groups = groupIds;
}
public void excludeFromGroup(Integer groupId){
Hibernate.initialize(this.groups);
this.groups.remove(groupId);
}
}
现在,当我尝试调用类似的东西时:
Session session = sessionFactory.openSession();
MutablePerson personFromDb = (MutablePerson) personService.getMutablePersonById(0);
personFromDb.excludeFromGroup(group1.getId());
session.saveOrUpdate(personFromDb);
我得到一个例外:
Exception in thread "main" org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:704)
at org.hibernate.Hibernate.initialize(Hibernate.java:65)
at com.studytrails.tutorials.springhibernatejpa.MutablePerson.excludeFromGroup(MutablePerson.java:57)
at com.studytrails.tutorials.springhibernatejpa.TestSpringHibernateJpa.main(TestSpringHibernateJpa.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
告诉我,MutablePerson
类的@Transactional注释实际上不起作用,Hibernate也没有创建事务。
这是我的春季配置:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
据我从Hibernate文档和示例中所知,这个配置是正确的,它应该可以工作,但它没有。我是不是错过了另一个Hibernate的细微差别?
附言:我知道我可以在DAO或Service类中做到这一点,但不幸的是,我必须使用现有的API,它与实体的可变半部分中的组一起工作。
实体不由Spring管理。它们是通过hibernate或手动保存日期作为数据库操作的产物创建的。因此@Transactional注释将不起作用,因为它不是spring管理的bean。
Spring事务上下文包装了一个Spring托管bean并对其执行操作。它不能包装非Spring bean
可能的解决方案最好为服务层或至少Jpa Repository/Dao层移动事务上下文。