기본적으로 사람을 이미 만든 후에 테스트 세트에 새 TestEntity를 추가 할 수 있도록 만드는 방법은 무엇입니까? 또한, 어떻게 TestEntity의 컬렉션을 가진 사람을 추가 할 수 있습니까? 나는 Hibernate에 익숙하지 않기 때문에 이것은 매우 일반적인 유스 케이스처럼 보일 것이므로 뭔가를 놓치고 있어야한다고 느낀다. 내가 해봤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 :
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();
}
}
그리고 다음과 같이 호출 : 내 다오이 같은 방법을 추가는
(http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-parentchild.html 기준) 그러나
personDao.add(person);
personDao.addTest("username", test);
, I 이 오류가 발생합니다 : org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
시도 # 3 :
내 DAO를 엔티티 클래스에추가 @Transaction 주석과 내 응용 프로그램 컨텍스트에 다음 설정을 추가 : 나는 시도 # 2에서 만든 방법을 사용하여, 지금
<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 />
및 같은 방식으로 호출, 나는 stackoverflow 오류가 발생합니다.
그러나 수정 : PersonEntity의 hashCode 메소드에서 테스트 세트를 제거하면 작동합니다. 또한 person.addTest (test)를 사용할 수 있으며 person 엔티티를 지속하기 전에 person 엔티티에 콜렉션을 추가 할 수 있습니다. 그러나 실제로는 이것이 최선의 방법이 아닌 것처럼 보입니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 내가 추가 한 DAO 방법은 필요한 것보다 더 많은 전화를하는 것처럼 보입니다.
내 클래스 :
PERSON
@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);
}
}
TEST
@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());
}
}
[게시물에 서명 또는 태그 라인 사용] (http://stackoverflow.com/faq#signatures) – meagar