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 ?