@Entity 中的@Transactional 不起作用?
@Transactional in @Entity doesn't work?
进一步研究 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
class 的 @Transactional 注释实际上不起作用,Hibernate 没有创建事务。
这是我的 spring 配置的一部分:
<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 细微差别吗?
P.S。我知道我可以在 DAO 或服务 class 中完成此操作,但不幸的是我必须使用现有的 API,它与实体可变部分中的组一起工作。
实体不受 Spring 管理。它们是通过休眠或手动保存日期作为数据库操作的产物创建的。因此 @Transactional 注释将不起作用,因为这不是 spring 托管 bean。
Spring 事务上下文环绕 spring 托管 bean 并对其进行操作。它不能包装非 spring beans
可能的解决方案
理想地为服务层或至少 Jpa Repository/Dao 层移动事务上下文。
进一步研究 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
class 的 @Transactional 注释实际上不起作用,Hibernate 没有创建事务。
这是我的 spring 配置的一部分:
<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 细微差别吗?
P.S。我知道我可以在 DAO 或服务 class 中完成此操作,但不幸的是我必须使用现有的 API,它与实体可变部分中的组一起工作。
实体不受 Spring 管理。它们是通过休眠或手动保存日期作为数据库操作的产物创建的。因此 @Transactional 注释将不起作用,因为这不是 spring 托管 bean。
Spring 事务上下文环绕 spring 托管 bean 并对其进行操作。它不能包装非 spring beans
可能的解决方案 理想地为服务层或至少 Jpa Repository/Dao 层移动事务上下文。