gpt4 book ai didi

java - Hibernate 向用户添加一本书

转载 作者:行者123 更新时间:2023-11-30 02:58:44 26 4
gpt4 key购买 nike

我在用户图书之间建立了多对多关系。现在,我想将现有书籍添加到 User_Books 中的现有用户,但每次执行时都会在 User 表中创建一个空的新用户。

       Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();

Books book = new Books();
book.setBookshareId(bookshareId);

User user1 = new User();
user1.setUser_id(userID);

user1.getBooks().add(book);
session.save(user1);

tx.commit();

用户:

@Entity
@Table(name="tblUser")
public class User implements Serializable{

@Id
@Column(name = "Id",unique = true,nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
private int Id;

@Column(name = "email")
private String email;

@Column(name = "password")
private String password;

@ManyToMany(mappedBy="user")
private List<Books> books = new ArrayList<Books>();

public void addBooks(Books book) {
this.books.add(book);
}

书籍:

@ManyToMany
@JoinTable(name = "User_Books",
joinColumns = { @JoinColumn(name = "Books_id")},
inverseJoinColumns = { @JoinColumn(name = "User_id")})
private List<User> user = new ArrayList<User>();

public void addUser(User user) {
this.user.add(user);
}

@穆罕默德·纳布利当我执行这个时,它找到了它们,但是当我查看 User_Books 时,我什么也没看到。

                  DEBUG Collections - Collection found: [entity.bookdetail.Books.user#946669], was: [entity.bookdetail.Books.user#946669] (uninitialized)
12:00:37 web.1 | DEBUG Collections - Collection found: [entity.users.User.books#6], was: [entity.users.User.books#6] (uninitialized)
12:00:37 web.1 | DEBUG AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
12:00:37 web.1 | DEBUG AbstractFlushingEventListener - Flushed: 0 (re)creations, 1 updates, 0 removals to 2 collections
12:00:37 web.1 | DEBUG EntityPrinter - Listing entities:
12:00:37 web.1 | DEBUG EntityPrinter - entity.bookdetail.Books{copyright=1998-01-01 00:00:00.0, images=0, author=J. K. Rowling, briefSynopsis=Second in ...
12:00:37 web.1 | DEBUG EntityPrinter - entity.users.User{password=u1t1, books=<uninitialized>, Id=6, email=user1@test1.com}
12:00:37 web.1 | DEBUG JdbcTransaction - committed JDBC Connection
12:00:37 web.1 | DEBUG LogicalConnectionImpl - Releasing JDBC connection
12:00:37 web.1 | DEBUG LogicalConnectionImpl - Released JDBC connection

最佳答案

我希望这会起作用,

您的数据库中有用户和书籍,不要使用 new 来创建用户,您必须直接从数据库中获取它。

将此行添加到您的用户类的 ManyToMany 注释

@ManyToMany(mappedBy="user",cascade = {CascadeType.ALL})  
private List<Books> books = new ArrayList<Books>();

试试这个:

Books book = (Books) session.get(Books.class, book_id);
User user = (User) session.get(User.class, user_id);
List<Books> books=user.getBooks();
books.add(book);
user.setBooks(books);

session.saveOrUpdate(user);

关于java - Hibernate 向用户添加一本书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com