进一步深入Hibernate我偶然发现以下情况: 我有一个@Entity人
@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的细微差别吗?
P.S。我知道我可以在DAO或Service类中完成此操作,但不幸的是我必然会使用现有的API,它可以与实体的可变半部分中的组一起使用。
答案 0 :(得分:0)
实体不受Spring管理。它们是通过休眠或手动创建数据库操作的产品,以保存日期。因此@Transactional注释不起作用,因为它不是一个Spring托管bean。
Spring事务上下文包装一个spring托管bean并对其进行操作。它不能包裹非春豆
可能的解决方案 理想情况下,为服务层或至少Jpa Repository / Dao层移动事务上下文。