jpa持久化实体与多个关系并插入null而不是id

时间:2014-10-29 05:27:07

标签: java oracle hibernate jpa

我正试图坚持与INSTITType类有很多关系的Institution类对象,并且在持久化时插入null,并且还在数据库表中添加了一些列。

@Table(name="FI")
public class Institution implements Serializable{

    @Id
    @GeneratedValue
    private int id;

    private String code;
    private String name;
    private String address;
    private String phone;
    private String email;
    private String fax;
    private InstitutionType type;
    private Date regDate;

    @ManyToOne
    @JoinColumn(name="FI_TYPE_ID", referencedColumnName="ID", nullable=false)
    public InstitutionType getType(){ return this.type; }
}

@Table(name="FI_TYPES")
public class InstitutionType implements Serializable{

    @Id
    @GeneratedValue
    private int id;

    private String code;
    private String name;

    public InstitutionType(){};

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

    public int getId(){ return this.id; }

    public void setCode(String code){ this.code = code; }

    public String getCode(){ return this.code; }

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

    public String getName(){ return this.name; }
}

这是oracle数据库表模型:

CREATE TABLE "SYSTEM"."FI" 
   (    "ID" NUMBER NOT NULL ENABLE, 
    "CODE" VARCHAR2(20 BYTE) NOT NULL ENABLE, 
    "NAME" VARCHAR2(255 BYTE) NOT NULL ENABLE, 
    "ADDRESS" VARCHAR2(255 BYTE), 
    "TELEPHONE" VARCHAR2(255 BYTE), 
    "EMAIL" VARCHAR2(255 BYTE), 
    "FAX" VARCHAR2(255 BYTE), 
    "FI_TYPE_ID" NUMBER NOT NULL ENABLE, 
    "REG_DATE" TIMESTAMP (6) NOT NULL ENABLE, 
    "PHONE" VARCHAR2(255 CHAR), 
     CONSTRAINT "FI_PK" PRIMARY KEY ("ID"),
     CONSTRAINT "FI_UK1" UNIQUE ("CODE"), 
     CONSTRAINT "FI_FK1" FOREIGN KEY ("FI_TYPE_ID")
      REFERENCES "SYSTEM"."FI_TYPES" ("ID") ENABLE
   )

这是跟踪:

javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
    at com.vamekh.server.InstitutionServiceImpl.addInstitution(InstitutionServiceImpl.java:28)
    at com.vamekh.client.GwtTestproject3.addTemplateTest(GwtTestproject3.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
    ... 24 more
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
    ... 24 more
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("SYSTEM"."FI"."FI_TYPE_ID")

    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10500)
    at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:230)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 32 more

运行GWT测试用例后,表格改为:

CREATE TABLE "SYSTEM"."FI" 
   (    "ID" NUMBER NOT NULL ENABLE, 
    "CODE" VARCHAR2(20 BYTE) NOT NULL ENABLE, 
    "NAME" VARCHAR2(255 BYTE) NOT NULL ENABLE, 
    "ADDRESS" VARCHAR2(255 BYTE), 
    "TELEPHONE" VARCHAR2(255 BYTE), 
    "EMAIL" VARCHAR2(255 BYTE), 
    "FAX" VARCHAR2(255 BYTE), 
    "FI_TYPE_ID" NUMBER NOT NULL ENABLE, 
    "REG_DATE" TIMESTAMP (6) NOT NULL ENABLE, 
    "PHONE" VARCHAR2(255 CHAR),
    "REGDATE" TIMESTAMP (6), //this column added
    "TYPE" RAW(255), //this column added
     CONSTRAINT "FI_PK" PRIMARY KEY ("ID"), 
     CONSTRAINT "FI_UK1" UNIQUE ("CODE"), 
     CONSTRAINT "FI_FK1" FOREIGN KEY ("FI_TYPE_ID")
      REFERENCES "SYSTEM"."FI_TYPES" ("ID") ENABLE
   )

哦,我忘了这是log4j日志:

    09:23:07,914  INFO SchemaUpdate:155 - Running hbm2ddl schema update
    09:23:07,915  INFO SchemaUpdate:167 - fetching database metadata
    09:23:08,281  INFO SchemaUpdate:179 - updating schema
    09:23:08,394  INFO TableMetadata:65 - table found: SYSTEM.FI
    09:23:08,395  INFO TableMetadata:66 - columns: [id, fi_type_id, phone, fax, email, address, regdate, name, code, type, reg_date, telephone]
    09:23:08,395  INFO TableMetadata:68 - foreign keys: [fi_fk1]
    09:23:08,395  INFO TableMetadata:69 - indexes: [fi_uk1, fi_pk]
    09:23:08,415  INFO TableMetadata:65 - table found: SYSTEM.FI_TYPES
    09:23:08,415  INFO TableMetadata:66 - columns: [id, name, code]
    09:23:08,415  INFO TableMetadata:68 - foreign keys: []
    09:23:08,416  INFO TableMetadata:69 - indexes: [fi_types_uk1, fi_types_pk]
    09:23:08,435  INFO TableMetadata:65 - table found: SYSTEM.RETURNS
    09:23:08,435  INFO TableMetadata:66 - columns: [template, id, fi_id, description, template_id, code, institution]
    09:23:08,435  INFO TableMetadata:68 - foreign keys: [fi_template_fk2, fi_id_fk1]
    09:23:08,436  INFO TableMetadata:69 - indexes: [return_uk1, return_pk]
    09:23:08,454  INFO TableMetadata:65 - table found: SYSTEM.TEMPLATES
    09:23:08,454  INFO TableMetadata:66 - columns: [id, schedule, description, code]
    09:23:08,454  INFO TableMetadata:68 - foreign keys: []
    09:23:08,455  INFO TableMetadata:69 - indexes: [templates_uk1, templates_pk]
    09:23:08,456  INFO SchemaUpdate:217 - schema update complete
Hibernate: select institutio0_.id as id2_0_, institutio0_.code as code2_0_, institutio0_.name as name2_0_ from FI_TYPES institutio0_ where institutio0_.id=?
    09:23:08,584 TRACE BasicBinder:82 - binding parameter [1] as [INTEGER] - 3
    09:23:08,591 TRACE BasicExtractor:71 - found [1] as column [code2_0_]
    09:23:08,592 TRACE BasicExtractor:71 - found [test test] as column [name2_0_]
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into FI (address, code, email, fax, name, phone, regDate, type, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
    09:23:08,620 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - 
    09:23:08,621 TRACE BasicBinder:82 - binding parameter [2] as [VARCHAR] - 0003
    09:23:08,621 TRACE BasicBinder:82 - binding parameter [3] as [VARCHAR] - 
    09:23:08,622 TRACE BasicBinder:82 - binding parameter [4] as [VARCHAR] - 
    09:23:08,622 TRACE BasicBinder:82 - binding parameter [5] as [VARCHAR] - firssq
    09:23:08,622 TRACE BasicBinder:82 - binding parameter [6] as [VARCHAR] - 
    09:23:08,630 TRACE BasicBinder:82 - binding parameter [7] as [TIMESTAMP] - Fri Mar 18 04:58:31 GET 2005
    09:23:08,632 TRACE BasicBinder:82 - binding parameter [8] as [VARBINARY] - com.vamekh.shared.InstitutionType@1791b2c
    09:23:08,633 TRACE BasicBinder:82 - binding parameter [9] as [INTEGER] - 16
    09:23:08,667  WARN JDBCExceptionReporter:233 - SQL Error: 1400, SQLState: 23000
    09:23:08,668 ERROR JDBCExceptionReporter:234 - ORA-01400: cannot insert NULL into ("SYSTEM"."FI"."FI_TYPE_ID")

    09:23:08,668  WARN JDBCExceptionReporter:233 - SQL Error: 1400, SQLState: 23000
    09:23:08,668 ERROR JDBCExceptionReporter:234 - ORA-01400: cannot insert NULL into ("SYSTEM"."FI"."FI_TYPE_ID")

addInstitution: `     EntityManager em;

public InstitutionDTO addInstitution(InstitutionDTO instDTO) {

    em = JpaUtil.getEntityManager().createEntityManager();
    em.getTransaction().begin();
    Institution inst = new Institution(instDTO);
    em.persist(inst);
    em.getTransaction().commit();

    return instDTO;

}`

测试用例:

@Test public void addTemplateTest(){ InstitutionTypeServiceImpl typeRpc = new InstitutionTypeServiceImpl(); InstitutionServiceImpl instRpc = new InstitutionServiceImpl(); InstitutionTypeDTO typeDTO = typeRpc.getInstitutionType(3); InstitutionDTO instDTO = new InstitutionDTO("0003", "test", "", "", "", "", typeDTO, new java.sql.Date(1111111111111L)); InstitutionDTO tmpDTO = instRpc.addInstitution(instDTO); assert(tmpDTO != null); }

我在创建了insitution时检查了调试器,type is not null

0 个答案:

没有答案
相关问题