尝试使用hibernate.Integrity约束违规将数据插入子表时出错

时间:2017-06-25 06:55:19

标签: java mysql oracle hibernate spring-data-jpa

父表:(Customer_one)使用了hibernate Cascade和CascadeType。尝试在app.persistAnnoatedLists()方法中提交事务时出现约束违规错误。 (我能够将数据插入父表但无法将数据插入子表),错误消息是(似乎子表中的外键未使用父键映射,使用Oracle DB和主键生成使用的类型是序列):

表: CUSTOMER_ONE: PK ---> custnone。 EVENTREQUEST_ONE: PK ----> eventnoone。 FK ----> custnoone。

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3067)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at icdabs.econd.icdab.App.persistAnnotatedLists(App.java:75)
    at icdabs.econd.icdab.App.main(App.java:96)
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (SYSTEM.EVENTREQUEST_FK_ONE) violated - parent key not found

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:943)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1075)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3897)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1361)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)

customer_one:

@Entity
@Table(name = "CUSTOMER_ONE")
public class Customer_one {
    @Id
    @Column(name = "custnoone", nullable = false)
    @SequenceGenerator(name="mysequence", sequenceName="my_seq",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="mysequence")
    private int custnoone;

private String custnameone;
private String addressone;
private String Internalone;
private String contactone;
private int phoneone;
private String cityone;
private String stateone;
private int zipone;

    @OneToMany
    @JoinColumn(name="custnoone")
    @Cascade(CascadeType.SAVE_UPDATE)
    private List<EventRequest_one> EventRequest_ones = new ArrayList<EventRequest_one>(); 



    public List<EventRequest_one> getEventRequest_ones() {
        return EventRequest_ones;
    }
    public void setEventRequest_ones(List<EventRequest_one> eventRequest_ones) {
        EventRequest_ones = eventRequest_ones;
    }

EventRequest_one:

@Entity
@Table(name = "EVENTREQUEST_ONE")
public class EventRequest_one {
    @Id
    @Column(name = "eventnoone", nullable = false)
    @SequenceGenerator(name="mysequence", sequenceName="my_seq",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="mysequence")
    private int eventnoone;
        private Date dateheldone;
    private Date datereqone;
    private int facnoone;
    private Date dateauthone;
    private String statusone;
    private int estcostone;
    private int estaudienceone;
    private int budnoone;
//  @Transient
    @Column(name="CUSTNOONE")
    private int custnoone;


    public EventRequest_one() {
    }

    public EventRequest_one( Date dateheld, Date datereq, Date dateauth, String status, int estcost,
            int estaudience, int budno, int facno) {
    //  setCustnoone(custno);
    //  setEventnoone(eventno);
        setDateheldone(dateheld);
        setDatereqone(datereq);
        setDateauthone(dateauth);
        setStatusone(status);
        setEstcostone(estcost);
        setEstaudienceone(estaudience);
        setBudnoone(budno);
        setFacnoone(facno);
    }


public int hashCode() {
        return 31;
    }
}

Main(App.java):

public class App 
{
    private static SessionFactory factory;
    private static ServiceRegistry registry;

    static {
        Configuration config = new Configuration().configure("hibernate.cfg.xml"); 
          registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
          factory=config.buildSessionFactory(registry);
    }

    @SuppressWarnings("deprecation")
    private void persistAnnotatedLists(){
        Configuration config = new Configuration().configure("hibernate.cfg.xml");

          registry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
          factory=config.buildSessionFactory(registry);

        Session session = factory.getCurrentSession(); 
        session.beginTransaction();
        Customer_one customer = new Customer_one(); 
        customer.setAddressone("address123");
        customer.setCityone("lansing");
        customer.setContactone("1632 ne ave");
        customer.setCustnameone("john");
    //    customer.setCustnoone(12);
        customer.setInternalone("I");
        customer.setPhoneone(122345679);
        customer.setStateone("OR");
        customer.setZipone(97654);

        List<EventRequest_one> er = new ArrayList<EventRequest_one>();
         er.add(new EventRequest_one(new Date(92,5,1),new Date(91,5,1),new Date(93,5,1),"av",123,456,124,12));

    session.save(customer);
         customer.setEventRequest_ones(er);

    for(EventRequest_one EventRequest_Ones : er){
            System.out.println("inside for loop");
        session.save(EventRequest_Ones);

        }


        session.getTransaction().commit();
        session.close();
    }
public static void main (String[] args){
        App app = new App();

        app.persistAnnotatedLists();
    //  app.retrieveList();
    }
}

0 个答案:

没有答案