Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在多对多联接表中插入记录而不首先进行联接?_Hibernate_Jpa_Relationship - Fatal编程技术网

Hibernate 如何在多对多联接表中插入记录而不首先进行联接?

Hibernate 如何在多对多联接表中插入记录而不首先进行联接?,hibernate,jpa,relationship,Hibernate,Jpa,Relationship,我有两个实体(用户和书籍),它们之间有多对多关系 @Entity public class Book { @Id @GeneratedValue private long id; @Column(unique = true) private String title; private int version; @ManyToOne(cascade = { CascadeType.ALL}) @JoinColumn(name

我有两个实体(用户和书籍),它们之间有多对多关系

@Entity
public class Book {

    @Id
    @GeneratedValue
    private long id;

    @Column(unique = true)
    private String title;

    private int version;

    @ManyToOne(cascade = { CascadeType.ALL})
    @JoinColumn(name = "author_id")
    private Author author;

    @ManyToMany(mappedBy = "books")
    private List<User> users;

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

@Entity
public class User {

    @Id
    @GeneratedValue
    long id;
    
    String name;
    
    @ManyToMany(cascade = { CascadeType.ALL})
    @JoinTable(name = "checkout", joinColumns = {
            @JoinColumn(name = "user_id", referencedColumnName = "id") }, inverseJoinColumns = {
                    @JoinColumn(name = "book_id", referencedColumnName = "id") })
    List<Book> books;


    public long getId() {
        return id;
    }


    public void setId(long id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public List<Book> getBooks() {
        return books;
    }


    public void setBooks(List<Book> books) {
        this.books = books;
    }
}
使用这种方法将涉及用户和签出表之间的连接


没有连接有什么方法可以做到这一点吗?

对于普通JPA或Hibernate,答案是否定的。您可以直接使用SQL,也可以使用Blaze Persistence,它支持JPA模型之上的集合DML:

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