-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ORA-02291:integrity constraint violated - parent key not f..
PostPosted: Thu Mar 12, 2009 3:54 am 
Newbie

Joined: Thu Mar 12, 2009 3:38 am
Posts: 2
Hello, I'm new to Hibernate and I spent much time trying to solve this problem. I have 2 classes : City and Country

Code:
public class City implements Serializable
{
   private static final long serialVersionUID = 9113236363962444882L;
   private Country country;
   private String name;
   private Long id;
   
   public City() {   }

... getters and setters
}


Code:
public class Country implements Serializable 
{
   private static final long serialVersionUID = -3450873498216753659L;
   
   public Country() {}

   private String name;
   private Long id;
... getters and setters
}


and this is my mappings:
Code:
<hibernate-mapping package="testapp.hibernate.model">
<class name="testapp.hibernate.model.City" table="CITY">

  <id column="ID" name="id">
   <generator class="sequence">
    <param name="sequence">city_sequence</param>
   </generator>
  </id>

  <property name="name"/>
  <many-to-one cascade="all"
   class="testapp.hibernate.model.Country" column="COUNTRY_ID"  name="country"/>

</class>
</hibernate-mapping>


Code:
<hibernate-mapping package="com.epam.testapp.hibernate.model">
<class name="testapp.hibernate.model.Country" table="COUNTRY">
  <id column="ID" name="id">
    <generator class="sequence">
       <param name="sequence">country_sequence</param>
    </generator>
  </id>

  <property name="name"/>
 
</class>
</hibernate-mapping>



code to run:
Code:
public static void main(String[] args) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      Transaction tx = session.beginTransaction();

      City city = new City();
      city.setName("Minsk");
      Country country = new Country("Belarus");
      city.setCountry(country);

      session.save(city);

      tx.commit();
}


My log:

Code:
Hibernate: select country_sequence.nextval from dual
Hibernate: select city_sequence.nextval from dual
Hibernate: insert into COUNTRY (name, ID) values (?, ?)
Hibernate: insert into CITY (name, COUNTRY_ID, ID) values (?, ?, ?)
09:40:13,085  WARN JDBCExceptionReporter:100 - SQL Error: 2291, SQLState: 23000
09:40:13,085 ERROR JDBCExceptionReporter:101 - ORA-02291: integrity constraint (HIBER.FK_COUNTRY) violated - parent key not found

09:40:13,085  WARN JDBCExceptionReporter:100 - SQL Error: 2291, SQLState: 23000
09:40:13,085 ERROR JDBCExceptionReporter:101 - ORA-02291: integrity constraint (HIBER.FK_COUNTRY) violated - parent key not found

09:40:13,085 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
   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:266)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
   at com.epam.testapp.hibernate.local.Runner.saveAddres(Runner.java:58)
   at com.epam.testapp.hibernate.local.Runner.main(Runner.java:63)
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (HIBER.FK_COUNTRY) violated - parent key not found

   at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
   at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:8739)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 9 more



Any solutions ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 1:22 pm 
Newbie

Joined: Thu Mar 12, 2009 3:38 am
Posts: 2
up


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.