0

I have two tables Students and Books , with a many to many relationship. The code of both are given below. Now when I try to run the code I get the error.

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'acme.book_stud' doesn't exist Error Code: 1146 Call: INSERT INTO book_stud (idStudents, idBooks) VALUES (?, ?) bind => [2 parameters bound]

It seems like JPA is trying to write to a juction table which does not exist (in this case it assumes a junction table books_students is already created so it doesnt create one.). It works if I create a books_students but I dont want to do that since its JPA responsibility to create it. Is there a way in which I could explicitly tell it to create one. ? (I am taking a wild guess here - but I guess when creating a persitance unit I specified "none" I think thats why it didnt create that table . Am I correct ? Anyways here are my Student and Books Classes

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 + " ]";
    }

}

STUDENT CLASS

@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 + " ]";
    }

}
4

0 回答 0