为什么JPA在这种情况下不生成连接联结表

时间:2012-07-29 04:03:10

标签: jpa jpa-2.0

我有两张桌子学生和书籍,有很多关系。两者的代码如下。现在,当我尝试运行代码时,我收到错误。

异常[EclipseLink-4002](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'acme.book_stud'不存在 错误代码:1146 调用:INSERT INTO book_stud(idStudents,idBooks)VALUES(?,?)     bind => [2个参数绑定]

似乎JPA正在尝试写入不存在的juction表(在这种情况下,它假定已经创建了一个联结表 books_students ,因此它不会创建一个。)。如果我创建一个books_students但我不想这样做,因为它的JPA责任创建它。有没有一种方法可以明确告诉它创建一个。 ? (我在这里做了一个疯狂的猜测 - 但我想在创建一个持久单元时我指定“无”我认为这就是为什么它没有创建那个表。我是否正确?无论如何这里是我的学生和书籍类

BOOKS CLASS

@Entity
@Table(name = "books")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Books.findAll", query = "SELECT b FROM Books b"),
    @NamedQuery(name = "Books.findByIdBooks", query = "SELECT b FROM Books b WHERE b.idBooks = :idBooks"),
    @NamedQuery(name = "Books.findByBookName", query = "SELECT b FROM Books b WHERE b.bookName = :bookName"),
    @NamedQuery(name = "Books.findByBookType", query = "SELECT b FROM Books b WHERE b.bookType = :bookType")})
public class Books implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "idBooks")
    private String idBooks;
    @Size(max = 45)
    @Column(name = "BookName")
    private String bookName;
    @Size(max = 45)
    @Column(name = "BookType")
    private String bookType;


    /******************************************ADDED **********************/
    @ManyToMany
    @JoinTable(name = "book_stud", 
    joinColumns = { @JoinColumn(name = "idStudents") }, 
    inverseJoinColumns = { @JoinColumn(name = "idBooks") })
    /**************************************ENDED*****************************/


    public Books() {
    }

    public Books(String idBooks) {
        this.idBooks = idBooks;
    }

    public String getIdBooks() {
        return idBooks;
    }

    public void setIdBooks(String idBooks) {
        this.idBooks = idBooks;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBookType() {
        return bookType;
    }

    public void setBookType(String bookType) {
        this.bookType = bookType;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idBooks != null ? idBooks.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Books)) {
            return false;
        }
        Books other = (Books) object;
        if ((this.idBooks == null && other.idBooks != null) || (this.idBooks != null && !this.idBooks.equals(other.idBooks))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "domain.Books[ idBooks=" + idBooks + " ]";
    }

}

学生课程

@Entity
@Table(name = "students")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "StudentEnroll.findAll", query = "SELECT s FROM StudentEnroll s"),
    @NamedQuery(name = "StudentEnroll.findByIdStudents", query = "SELECT s FROM StudentEnroll s WHERE s.idStudents = :idStudents"),
    @NamedQuery(name = "StudentEnroll.findByName", query = "SELECT s FROM StudentEnroll s WHERE s.name = :name"),
    @NamedQuery(name = "StudentEnroll.findByRoll", query = "SELECT s FROM StudentEnroll s WHERE s.roll = :roll"),
    @NamedQuery(name = "StudentEnroll.findBySsn", query = "SELECT s FROM StudentEnroll s WHERE s.ssn = :ssn"),
    @NamedQuery(name = "StudentEnroll.findByProgram", query = "SELECT s FROM StudentEnroll s WHERE s.program = :program")})
public class StudentEnroll implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 40)
    @Column(name = "idStudents")
    private String idStudents;
    @Size(max = 45)
    @Column(name = "Name")
    private String name;
    @Column(name = "Roll")
    private Integer roll;
    @Column(name = "SSN")
    private Integer ssn;
    @Size(max = 45)
    @Column(name = "Program")
    private String program;
    @JoinColumn(name = "CustomerID", referencedColumnName = "UserID")
    @ManyToOne
    private Customer customerID;
    //@OneToMany(mappedBy = "studentRoll")
    @OneToMany(mappedBy = "studentRoll",cascade = CascadeType.REMOVE)//added REMOVE
    private Collection<Subject> subjectCollection;



    /**************************ADDED*****************************/
    @ManyToMany
    @JoinTable(name = "book_stud", 
    joinColumns = { @JoinColumn(name = "idBooks") }, 
    inverseJoinColumns = { @JoinColumn(name = "idStudents") })
    /**********************************END**********************/

    public StudentEnroll() {
    }

    public StudentEnroll(String idStudents) {
        this.idStudents = idStudents;
    }

    public String getIdStudents() {
        return idStudents;
    }

    public void setIdStudents(String idStudents) {
        this.idStudents = idStudents;
    }

    public String getName() {
        return name;
    }

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

    public Integer getRoll() {
        return roll;
    }

    public void setRoll(Integer roll) {
        this.roll = roll;
    }

    public Integer getSsn() {
        return ssn;
    }

    public void setSsn(Integer ssn) {
        this.ssn = ssn;
    }

    public String getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program = program;
    }

    public Customer getCustomerID() {
        return customerID;
    }

    public void setCustomerID(Customer customerID) {
        this.customerID = customerID;
    }

    @XmlTransient
    public Collection<Subject> getSubjectCollection() {
        return subjectCollection;
    }

    public void setSubjectCollection(Collection<Subject> subjectCollection) {
        this.subjectCollection = subjectCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idStudents != null ? idStudents.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof StudentEnroll)) {
            return false;
        }
        StudentEnroll other = (StudentEnroll) object;
        if ((this.idStudents == null && other.idStudents != null) || (this.idStudents != null && !this.idStudents.equals(other.idStudents))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "domain.StudentEnroll[ idStudents=" + idStudents + " ]";
    }

}

0 个答案:

没有答案