Hi I'm trying to insert a number of records into a database
The team persistent class comes first and it has a many-to-one relationship with the Country persistent class.
The team class has a cascade-all attribute set, and when i use SaveOrUpdate on the session it was my hope that it would find out if the many-to-one relationship existed with the country then save or take the reference key as necessary.
However as i insert team after team, hibernate seems to continually try to insert new countries until it generates a constraint violation exception.
I've checked the Wiki and the reference manual. I've tried changing batching attributes, changing the dynamic-insert attribute,clearing and flushing session inside the loop, checked my equals and hashcode methods.
What am i doing wrong?
Hibernate version:
3.1.3
Mapping documents:
<many-to-one name="country"
class="arb.persistance.object.nameType.Country" column="country_id"
cascade="all" />
.......
<hibernate-mapping>
<class name="arb.persistance.object.nameType.Country" table="country" dynamic-insert="true">
<id name="id" type="int" column="id">
<generator class="seqhilo">
<param name="sequence">ids</param>
<param name="max_lo">100</param>
</generator>
</id>
<property name="name" column="name" type="string" unique="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
s = HibernateUtil.currentSession();
for (Iterator ii = utils
.getCollection(ds, TeamBean .class)
.iterator(); ii.hasNext();) {
s.beginTransaction();
TeamBean bmBean = (TeamBean ) ii.next();
bmBean.setCountry(country[i++]);
s.saveOrUpdate(bmBean);
}
s.getTransaction().commit();
Full stack trace of any exception that occurs:
Batch entry 0 insert into public.country (name, id) values (Malta, 206) was aborted. Call getNextException to see the cause.
- SQL Error: 0, SQLState: 23505
- ERROR: duplicate key violates unique constraint "country_name_key"
- 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:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:91)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:171)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2048)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
Name and version of the database you are using:
postgres 1.4.2
The generated SQL (show_sql=true):
into public.country (name, id) values (Malta, 206)
Debug level Hibernate log excerpt:
|