1
私は、人のManyToMany関係を持っています。これは、Hibernateアノテーションで説明しようとしています。私もこれについてテストを作成しましたが、問題は関係が保存されないということです。私が間違っていた場所を見つけるのを助けてください!休止状態でManyToMany自己参照関係を保存できません
エンティティ:
@Entity(name = "person")
@Table(appliesTo = "person", indexes = {
@org.hibernate.annotations.Index(name = "ix_uuid", columnNames = {"uuid"}),
@org.hibernate.annotations.Index(name = "ix_facebookId", columnNames = {"facebookId"})
})
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = AUTO)
private Long id;
private String uuid;
private String firstName;
private String lastName;
private String facebookId;
private String email;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "person_friend",
joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "friend_id", referencedColumnName = "id")
)
private Set<Person> persons = new HashSet<Person>();
@ManyToMany(mappedBy = "persons", cascade = CascadeType.ALL)
private Set<Person> friends = new HashSet<Person>();
public Person(String uuid, String firstName, String lastName, String facebookId, String email) {
this.uuid = uuid;
this.firstName = firstName;
this.lastName = lastName;
this.facebookId = facebookId;
this.email = email;
}
public Person() {
// Hibernate
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void addFriend(Person person){
if(!getFriends().contains(person)){
getFriends().add(person);
person.getPersons().add(this);
}
}
public void becomeFriendOf(Person person) {
if(!getPersons().contains(person)){
getPersons().add(person);
person.getFriends().add(this);
}
}
public Set<Person> getFriends() {
return friends;
}
public void setFriends(Set<Person> friends) {
this.friends = friends;
}
public Set<Person> getPersons() {
return persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", uuid='" + uuid + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", facebookId='" + facebookId + '\'' +
", email='" + email + '\'' +
'}';
}
}
PersonDao:
@Repository("personDao")
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao {
@Autowired
public PersonDaoImpl(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
@Override
public void save(Person person) {
getSession().save(person);
}
@Override
public Person getPerson(Long id) {
return (Person) getSession().get(Person.class, id);
}
@Override
public void saveFriendship(Friendship friendship) {
getSession().save(friendship);
}
}
をテスト:
@Test(groups = {"integration"})
@ContextConfiguration(locations = {"classpath:applicationContext-test.xml" })
public class PersonDaoImplTest extends AbstractTestNGSpringContextTests {
@Autowired
private PersonDao target;
@Test
public void loadDatabaseWithSomeInitialValues(){
System.out.println(applicationContext.toString());
Person person = new Person("12345abcde","test","test2","test.test", "[email protected]");
Person person2 = new Person("4567abcde","fest","fest","fest.fest", "[email protected]");
Person person3 = new Person("89105abcde","best","best","best.best", "[email protected]");
person.addFriend(person2);
person.addFriend(person3);
person2.becomeFriendOf(person);
person3.becomeFriendOf(person);
target.save(person);
}
}
}
述べた人物が保存されると人テーブルではなく、person_friendテーブル内の関係。どうして?
それは私がトランザクションとしてDAOに注釈を付ける必要がありました。テストでは十分ではありませんでした。 – jakob