外键列的JPA OneToOne映射约束错误

时间:2016-02-03 11:22:55

标签: jpa persistence jdeveloper ora-00904

我尝试使用我的工作教程编写代码。我有一个父类Emp和一个子类EmpAdd。我使用持久性将这些类映射到我的表。这些类具有来自我的表Emp和Empadd的实体。在进行CRUD操作时,我收到无效标识符的ORA-904错误。当我试图将值插入数据库时​​,标识符显示一个额外的列名。 如果只插入父表,代码就有效,但是如果插入子表,则会发生此错误。

教程来源:http://www.tutorialspoint.com/jpa/jpa_entity_relationships.htm

Error Code: 904
Call: INSERT INTO EMPADD (ADDID, ADDLINE1, CITY, HOUSENO, EMP_EMPID) VALUES (?, ?, ?, ?, ?)
    bind => [5 parameters bound]
Query: InsertObjectQuery(EmpADD id: 111 houseno: Street1 addline1: WORk city: BL)
Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "EMP_EMPID": invalid identifier

Error Code: 904
Call: INSERT INTO EMPADD (ADDID, ADDLINE1, CITY, HOUSENO, EMP_EMPID) VALUES (?, ?, ?, ?, ?)
    bind => [5 parameters bound]
Query: InsertObjectQuery(EmpADD id: 111 houseno: Street1 addline1: WORk city: BL)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:159)
    at project1.NewTest.main(NewTest.java:36)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00904: "EMP_EMPID": invalid identifier

Error Code: 904
Call: INSERT INTO EMPADD (ADDID, ADDLINE1, CITY, HOUSENO, EMP_EMPID) VALUES (?, ?, ?, ?, ?)
    bind => [5 parameters bound]
Query: InsertObjectQuery(EmpADD id: 111 houseno: Street1 addline1: WORk city: BL)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:902)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:964)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:633)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:560)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2055)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:306)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:377)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301)
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904)
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:803)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1790)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:227)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsForClassWithChangeSet(CommitManager.java:194)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:139)
    at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4263)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:278)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:134)
    ... 1 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "EMP_EMPID": invalid identifier

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3694)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1354)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:892)
    ... 33 more

我用以下代码创建了我的表。

CREATE TABLE "EMP"
(   "EMPID" VARCHAR2(20 BYTE), 
"EMPNAME" VARCHAR2(20 BYTE), 
 PRIMARY KEY ("EMPID");


CREATE TABLE "EMPADD" 
(   "ADDID" VARCHAR2(20 BYTE), 
"HOUSENO" VARCHAR2(20 BYTE), 
"ADDLINE1" VARCHAR2(20 BYTE), 
"CITY" VARCHAR2(20 BYTE), 
"EEID" VARCHAR2(20 BYTE), 
 PRIMARY KEY ("ADDID"),
  FOREIGN KEY ("EEID")
  REFERENCES "EMP" ("EMPID") ENABLE

);

生成的insert命令有一个额外的列EMP_EMPID,这是我的外键。 对于Ref,我附上了完整的代码文件。

实体Java文件

     package project1;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;

    @Entity

    public class Emp {

        @Id
        @GeneratedValue( strategy= GenerationType.AUTO)
        private String empid;
        private String empname;


        public void setEmpid(String empid) {
            this.empid = empid;
        }

        public String getEmpid() {
            return empid;
        }

        public void setEmpname(String empname) {
            this.empname = empname;
        }

        public String getEmpname() {
            return empname;
        }

        public Emp() {
            super();
        }

        @Override
        public String toString(){
            return " Emp id:"+empid+" Empname:"+empname;
        }
    }


package project1;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;


@Entity

public class Empadd {
    @Id
@GeneratedValue(strategy=GenerationType.AUTO)
    private String addid;
    private String houseno;
    private String addline1;
    private String city;
    @OneToOne 
    private Emp emp;

    public Empadd(String addid,String houseno,String addline1,String city){
        this.addid=addid;
        this.houseno=houseno;
        this.addline1=addline1;
        this.city=city;
        }

    public void setAddid(String addid) {
        this.addid = addid;
    }

    public String getAddid() {
        return addid;
    }

    public void setHouseno(String houseno) {
        this.houseno = houseno;
    }

    public String getHouseno() {
        return houseno;
    }

    public void setAddline1(String addline1) {
        this.addline1 = addline1;
    }

    public String getAddline1() {
        return addline1;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setEmp(Emp emp) {
        this.emp = emp;
    }

    public Emp getEmp() {
        return emp;
    }

    public Empadd() {
        super();
    }

    public String toString(){
        return "EmpADD id: "+addid+" houseno: "+houseno+" addline1: "+addline1 +" city: "+city;
    }
}

Persistence.xml文件

<?xml version="1.0" encoding="windows-1252" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
<persistence-unit name="link11" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<class>project1.Emp</class>
<class>project1.Empadd</class>
    <properties>
       <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
        <property name="javax.persistence.jdbc.user" value="-------"/>
        <property name="javax.persistence.jdbc.password" value="------"/>
    </properties>
</persistence-unit>
          </persistence>

主类文件

package project1;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class NewTest {

    public static void main(String[] args){
        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "link11" );
           EntityManager entitymanager = emfactory.createEntityManager( );
           entitymanager.getTransaction( ).begin( );

   //Create Employee

           Emp emp = new  Emp();
           emp.setEmpid("00001");
            emp.setEmpname("RICK");

     //Store Employee

           entitymanager.persist(emp);

     // Create Emp address
           Empadd empadd = new Empadd();
           empadd.setAddid("111");
           empadd.setHouseno("Street1");
           empadd.setAddline1("WORk");
           empadd.setCity("BL");
           empadd.setEmp(emp);  //add emp to empadd

           System.out.println(empadd.toString()+emp.toString());

     //Store Emp Address
           entitymanager.persist(empadd);
           entitymanager.getTransaction( ).commit( );
            entitymanager.close( );
            emfactory.close( );
    }

}

0 个答案:

没有答案