0

これは私のポジョ、ユーザー、およびブックです

@Entity
public class User {
private int id;
private String username;
private String userage;
private String useremail;
private String usergender;
private Set<Book> books = new HashSet<Book>();

@Id
@GeneratedValue
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getUserage() {
    return userage;
}
public void setUserage(String userage) {
    this.userage = userage;
}
public String getUseremail() {
    return useremail;
}
public void setUseremail(String useremail) {
    this.useremail = useremail;
}
public String getUsergender() {
    return usergender;
}
public void setUsergender(String usergender) {
    this.usergender = usergender;
}

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name="user_book")
public Set<Book> getBooks() {
    return books;
}
public void setBooks(Set<Book> books) {
    this.books = books;
}
public void addBooks(Book book) {
    getBooks().add(book);
}
public void removeBooks(Book book) {
    getBooks().remove(book);
}
}

@Entity
public class Book {
private int bookid;
private String bookname;
private String bookauthor;
private String bookstatue;
private Set<User> users = new HashSet<User>();

@ManyToMany(mappedBy="books", fetch = FetchType.EAGER)
public Set<User> getUsers() {
    return users;
}
public void setUsers(Set<User> users) {
    this.users = users;
}
@Id
@GeneratedValue
public int getBookid() {
    return bookid;
}
public void setBookid(int bookid) {
    this.bookid = bookid;
}
public String getBookname() {
    return bookname;
}
public void setBookname(String bookname) {
    this.bookname = bookname;
}
public String getBookauthor() {
    return bookauthor;
}
public void setBookauthor(String bookauthor) {
    this.bookauthor = bookauthor;
}
public String getBookstatue() {
    return bookstatue;
}
public void setBookstatue(String bookstatue) {
    this.bookstatue = bookstatue;
}
}

Dao ファイル内: 次の操作を実行したい:

public boolean userReturnBookInDB(Book bookBean, User userBean) {
    // TODO Auto-generated method stub
    boolean remove;
    try {
        begin();
        ***userBean.removeBooks(bookBean);***       
        getSession().save(userBean);
        commit();
        remove = true;
    }
    catch (Exception e) {
        remove = false;
    }
    finally {       
        close();
    }
    return remove;
}

addBooks は正常に実行できますが、removeBooks を実行しようとすると、結合テーブルのメンバーが変更されません。誰かが私を助けてくれることを願っています。

4

1 に答える 1

0

User クラスの次のプロパティ ブックでは、ユーザーを保存するときに orphanRemoval=true を追加して、オーファンを削除する必要があります。

@ManyToMany(mappedBy="books", fetch = FetchType.EAGER,orphanRemoval=true)
public Set<User> getUsers() {
    return users;
}
于 2013-05-06T21:27:01.673 に答える