2011-10-19 97 views
1

我試圖用Hibernate註釋來描述之間的ManyToMany關係。我也爲此創建了一個測試,但問題是關係沒有保存。請幫我看看我做錯了什麼!無法保存休眠狀態下的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表中的關係。爲什麼?

回答

0

也許它與交易界限有關。儘量使你的測試方法@Transactional

+0

就是這樣,我不得不將我的DAO註釋爲事務性的。測試還不夠。 – jakob