简体   繁体   English

Eclipselink JPA ManyToOne 映射问题

[英]Eclipselink JPA ManyToOne mapping issue

Am new to JPA(eclipselink), am trying to use ManyToOne unidirectional mapping , i have superclass Student and subclass Book , when i try to save the object i end up with exceptions, the details are given below我是 JPA( ManyToOne unidirectional mapping ,我正在尝试使用ManyToOne unidirectional mapping ,我有superclass Studentsubclass Book ,当我尝试保存对象时,我最终遇到了异常,详细信息如下

Student is the Parent Class学生是家长班

@Entity
@Table(name="STUDENT")
@TableGenerator(name="student_s", initialValue=1)
public class Student {
    @Id 
    @Column(name="student_id")
    @GeneratedValue(strategy=GenerationType.TABLE ,generator="student_s")
    private int studentID;

    @Column(name="full_name",nullable=false)
    private String student_name;

    public int getStudentID() {
        return studentID;
    }

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public String getStudent_name() {
        return student_name;
    }

    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }

}

Book is the Child Class书是儿童班

@Entity
@Table(name="BOOK")
@TableGenerator(name="book_s", initialValue=1)
public class Book {
    @Id 
    @Column(name="book_id")
    @GeneratedValue(strategy=GenerationType.TABLE ,generator="book_s")
    private int bookID;

    @Column(name="book_name")
    private String bookName;

    @Column(name="student_id")
    private String studentID;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="student_id",referencedColumnName="student_id",insertable=false,updatable=false)
    private Student student;

    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 getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }


}

Main Class which executes the persist operation, while performing persist the unknown column exception is thrown.执行持久化操作的主类,在执行持久化时抛出未知列异常。

public class Main {
    public static void main(String args[]){     
        Student student = new Student();
        student.setStudent_name("Prem");

        Book book = new Book();
        book.setBookName("The Best Laid Plan");
        book.setStudent(student);

        EntityManagerFactory emf=Persistence.createEntityManagerFactory("premjpaeclipselink");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(book);
        em.getTransaction().commit();
        em.close();
        emf.close();
    }
}

Exception:例外:

]: sql: Connection(635083092)--INSERT INTO BOOK (book_id, book_name, student_id) VALUES (?, ?, ?)
    bind => [351, The Best Laid Plan, null]
[EL Fine]: sql: SELECT 1
[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'book_name' in 'field list'
Error Code: 1054
Call: INSERT INTO BOOK (book_id, book_name, student_id) VALUES (?, ?, ?)
    bind => [351, The Best Laid Plan, null]
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'book_name' in 'field list'
Error Code: 1054
Call: INSERT INTO BOOK (book_id, book_name, student_id) VALUES (?, ?, ?)
    bind => [351, The Best Laid Plan, null]
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at org.prem.jpa.eclipselink.Main.main(Main.java:31)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'book_name' in 'field list'
Error Code: 1054
Call: INSERT INTO BOOK (book_id, book_name, student_id) VALUES (?, ?, ?)
    bind => [351, The Best Laid Plan, null]
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:900)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:962)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:631)
    at org.eclipse.persistence.internal.databaseaccess.ParameterizedSQLBatchWritingMechanism.executeBatch(ParameterizedSQLBatchWritingMechanism.java:149)
    at org.eclipse.persistence.internal.databaseaccess.ParameterizedSQLBatchWritingMechanism.executeBatchedStatements(ParameterizedSQLBatchWritingMechanism.java:134)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.writesCompleted(DatabaseAccessor.java:1836)
    at org.eclipse.persistence.internal.sessions.AbstractSession.writesCompleted(AbstractSession.java:4244)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.writesCompleted(UnitOfWorkImpl.java:5594)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.acquireWriteLocks(UnitOfWorkImpl.java:1646)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitTransactionAfterWriteChanges(UnitOfWorkImpl.java:1614)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:284)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
    ... 1 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'book_name' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:890)
    ... 13 more

I can see this line from your exception stacktrace:我可以从您的异常堆栈跟踪中看到这一行:

Unknown column 'book_name' in 'field list ' Unknown column 'book_name' in 'field list ”中的Unknown column 'book_name' in 'field list

make sure column "book_name" in BOOK table is correct in database确保 BOOK 表中的列“book_name”在数据库中是正确的

i hope it helps !!!我希望它有帮助!!!

Change private String studentID;更改private String studentID; in Book class to private int studentID;在 Book 类到private int studentID; and also change respective getter and setter.并更改各自的 getter 和 setter。

Entity works well.实体运作良好。 It is the problem of column name.是列名的问题。 To make sure your DDL, you can use with为了确保您的 DDL,您可以使用

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> . <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

As @Sanket Pipariya said, you don't need正如@Sanket Pipariya 所说,你不需要

@Column(name="student_id")
private String studentID;

Because you already have Student Object ('student_id' column in Book table).因为您已经有了 Student 对象(Book 表中的“student_id”列)。 You are using insertable=false in Student attribute in Book Class so that when you persist Book object along with Student attribute, student_id column in Book table will be null.您在 Book Class 的 Student 属性中使用insertable=false ,以便当您将 Book 对象与 Student 属性一起持久化时,Book 表中的student_id列将为空。

This exception will be removed when you create table in database of both book and student.当您在 book 和 student 的数据库中创建表时,将删除此异常。 Then the records will be inserted.然后将插入记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM