![](/img/trans.png)
[英]Hibernate does not delete a DB record when an entity is removed from a collection
[英]How to add/delete elements from a collection on an entity in Hibernate?
基本上,我将如何做到这一点,以便在创建人员之后将新的 TestEntity 添加到测试集中? 另外,如何添加一个拥有 TestEntity 集合的人? 我是 Hibernate 的新手,所以我觉得我必须遗漏一些东西,因为这似乎是一个非常常见的用例。
我尝试过的一些事情:
尝试#1:
PersonEntity person = createPerson("username");
TestEntity test = new TestEntity();
test.setTestId("2342");
test.setTestName("test name");
personDao.add(person);
person.addTest(test);
这导致该人被保存但没有测试信息。 切换 add 和 addTest 不会改变任何东西。
尝试#2:
在我的 Dao 中添加这样的方法(基于http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-parentchild.ZFC35FDC70D5FC69D269883A822C )
public void addTest(String personId, TestEntity test)
{
PersonEntity entity = (PersonEntity) getHibernateTemplate().getSessionFactory().getCurrentSession().load(PersonEntity.class, personId);
if (entity != null)
{
test.setPerson(entity);
entity.getTest().add(test);
getHibernateTemplate().getSessionFactory().getCurrentSession().save(entity);
getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
}
}
并像这样调用:
personDao.add(person);
personDao.addTest("username", test);
但是,我收到此错误: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
尝试#3:
在我的 dao 和实体类中添加了 @Transaction 注释,并将以下配置添加到我的应用程序上下文中:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- org.springframework.transaction.jta.JtaTransactionManager org.springframework.jdbc.datasource.DataSourceTransactionManager -->
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
现在,使用我在尝试 #2 中创建的方法并以相同的方式调用它,我得到一个 stackoverflow 错误。
编辑更新:但是,如果我从 PersonEntity 中的 hashCode 方法中删除测试集,它会起作用。 我也可以使用 person.addTest(test) ,它会在持久化 person 实体之前负责向 person 实体添加一个集合。 但是,这似乎并不是最好的方法,不是吗? 完成这项工作的最佳方法是什么? 我添加的那个 dao 方法似乎会进行不必要的调用?
我的课程:
人
@Entity
@Table(name = "PERSON")
public class PersonEntity implements Serializable
{
private static final long serialVersionUID = -1699435979266209440L;
@Id
@Column(name = "PERSON_ID", length = 25, nullable = false)
private String personId;
@LazyCollection(value = LazyCollectionOption.FALSE)
@Cascade(CascadeType.ALL)
@OneToMany(targetEntity = TestEntity.class, mappedBy = "person")
@Where(clause="1=1")
private Set<TestEntity> test;
public void addTest(TestEntity testEntity)
{
testEntity.setPerson(this);
test.add(testEntity);
}
}
测试
@Entity
@Table(name = "TEST")
public class TestEntity implements Serializable
{
private static final long serialVersionUID = -6524488155196023818L;
@Id
@Column(name = "TEST_ID", length = 36, nullable = false)
private String testId;
@ManyToOne
@Cascade(CascadeType.ALL)
@Index(name = "TEST_PERSON_ID_INDEX")
@JoinColumn(name = "PERSON_ID")
@ForeignKey(name = "FKT1_PERSON_ID")
private PersonEntity person;
@Column(name = "TEST_NAME", length = 60, nullable = false)
private String testName;
}
PersonDaoHibernate
public class PersonDaoHibernate extends HibernateDaoSupport implements PersonDao
{
public String add(PersonEntity person)
{
getHibernateTemplate().merge(person);
return person.getPersonId();
}
public void delete(String id)
{
Object entity = getHibernateTemplate().get(PersonEntity.class, id);
if (entity != null)
{
getHibernateTemplate().delete(entity);
}
}
public PersonEntity getById(String id)
{
return getHibernateTemplate().get(PersonEntity.class, id.toUpperCase());
}
}
我认为您必须将方法名称更改为 setTest(...),因为 hibernate 在尝试对属性进行操作时遵循 java bean 约定。 改变它,我希望它应该能正常工作。 Rest 的代码看起来不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.