无法在休眠中保存ManyToMany自引用关系

时间:2011-10-19 11:41:44

标签: java hibernate spring many-to-many

我在 person 之间有一个ManyToMany关系,我试图用Hibernate注释来描述它。我也为此创建了一个测试,但问题是关系没有保存。请帮我找到我做错的地方!

实体:

@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", "test.test@gmail.com");
        Person person2 = new Person("4567abcde","fest","fest","fest.fest", "fest.fest@gmail.com");
        Person person3 = new Person("89105abcde","best","best","best.best", "best.best@gmail.com");

        person.addFriend(person2);
        person.addFriend(person3);

        person2.becomeFriendOf(person);
        person3.becomeFriendOf(person);

        target.save(person);
    }    
    }
}

如上所述,这些人员保存在表中,但未保存在 person_friend 表中的关系中。为什么呢?

1 个答案:

答案 0 :(得分:0)

也许它与交易边界有关。尝试制作测试方法@Transactional