Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用hibernate将数据插入子表时出错。完整性约束冲突_Java_Mysql_Oracle_Hibernate_Spring Data Jpa - Fatal编程技术网

Java 尝试使用hibernate将数据插入子表时出错。完整性约束冲突

Java 尝试使用hibernate将数据插入子表时出错。完整性约束冲突,java,mysql,oracle,hibernate,spring-data-jpa,Java,Mysql,Oracle,Hibernate,Spring Data Jpa,父表:(Customer_one)使用hibernate级联和级联类型。尝试在app.persistAnnoatedLists()方法中提交事务时发生约束冲突错误。(我能够将数据插入父表,但无法将数据插入子表),错误消息是(子表中的外键似乎没有映射到父键,使用的是Oracle DB,使用的主键生成类型是sequence): 表: 顾客第一: PK--->custnone。 事件请求(1): PK-->eventnoone。 FK-->custnoone Exception in thread "

父表:(Customer_one)使用hibernate级联和级联类型。尝试在app.persistAnnoatedLists()方法中提交事务时发生约束冲突错误。(我能够将数据插入父表,但无法将数据插入子表),错误消息是(子表中的外键似乎没有映射到父键,使用的是Oracle DB,使用的主键生成类型是sequence):

表: 顾客第一: PK--->custnone。 事件请求(1): 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)
顾客第一:

@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;
    }
Main(App.java):

公共类应用程序
{
私营静电厂;
私有静态服务注册表;
静止的{
Configuration config=new Configuration().configure(“hibernate.cfg.xml”);
registry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
factory=config.buildSessionFactory(注册表);
}
@抑制警告(“弃用”)
私有void persistedList(){
Configuration config=new Configuration().configure(“hibernate.cfg.xml”);
registry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
factory=config.buildSessionFactory(注册表);
Session Session=factory.getCurrentSession();
session.beginTransaction();
客户_-one客户=新客户_-one();
客户。setAddressone(“地址123”);
customer.setCityone(“兰辛”);
客户:setContactone(“东北大街1632号”);
客户。setCustnameone(“约翰”);
//客户:Setcustnone(12);
客户。setInternalone(“我”);
客户:setPhoneone(122345679);
客户。一方(“或”);
客户:setZipone(97654);
List er=new ArrayList();
添加(新事件请求单(新日期(92,5,1)、新日期(91,5,1)、新日期(93,5,1),“av”,123456124,12));
session.save(客户);
customer.setEventRequest_one(er);
对于(EventRequest\u一个EventRequest\u一个:er){
System.out.println(“内部为循环”);
session.save(EventRequest\u个);
}
session.getTransaction().commit();
session.close();
}
公共静态void main(字符串[]args){
App App=新App();
app.persistAnnotatedList();
//app.retrieveList();
}
}

我想你自己也提到了答案:我不知道错误在哪里。我检查了Oracle数据库以及主键和外键是否相互匹配并正确引用。因此,问题可能在于hibernate映射。你能告诉我我在地图上做错了什么吗@Rahulraja删除了EventRequest_one类和getter和setter中的custnoone字段。并将数据库FK字段约束NOT NULL更改为NULL。然后它工作了。为什么非空约束必须在数据库中的一个表中删除EventRequest中的外键。否则我会得到错误,因为foriegnkey不能为null。在Customer\u one实体中,映射应该是
@OneToMany(cascade=ALL,mappedBy=“Customer\u one”)私有列表EventRequest\u one
,在EventRequest\u一侧,映射应该如下所示:
@manytone@JoinColumn(name=“custnoone”)public Customer\u one客户\u 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;
    }
}
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();
    }
}