Hi everyone,
I'm getting an FK violation when trying to insert an entry in a Map, but
can't for the life of me figure out why.
Could anyone here see what I'm doing wrong? Am I doing something wrong with the <element> tag?
Hibernate version:
3.1
Mapping documents:
Code:
Table structure:
create Table Examinator_Kurs (Kurs_ID int not null,
Termin_ID int not null,
Person_ID int not null,
foreign key (Kurs_ID) references Kurs (KursID),
foreign key (Person_ID) references Person(PersonID)
)
Mappings:
<class name="daisy.shared.kurs.Kurs" table="Kurs" lazy="false">
<id name="kursID" column="KursID" unsaved-value="-1">
<generator class="native"/>
</id>
<map name="examiners" table="Examinator_Kurs" lazy="true" cascade="none" >
<meta attribute="field-description">
The examiners for this course, by term.
</meta>
<key column="Kurs_ID" not-null="true"/>
<map-key type="int" column="Termin_ID"/>
<element type="daisy.shared.person.Person" column="Person_ID" unique="false" not-null="true"/>
</map>
</class>
<class name="daisy.shared.person.Person" table="Person" lazy="true" batch-size="3">
<id name="personID" column="PersonID" type="int" unsaved-value="0">
<generator class="native"/>
</id>
</class>
Code between sessionFactory.openSession() and session.close():
public void setExaminer(Kurs kurs, int terminID, Person person) {
if (person == null) {
kurs.getExaminers().remove(terminID);
} else {
kurs.getExaminers().put(terminID, person);
}
kursDAO.save(kurs); //Just calls session.saveOrUpdate(kurs)
}
Full stack trace of any exception that occurs:
[13:27:36.130][ITMIRROR]INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK__Examinato__Perso__56C3C04D'. The conflict occurred in database 'SkarpKopia', table 'Person', column 'PersonID'.
[13:27:36.130] at com.inet.tds.a.a(Unknown Source)
[13:27:36.130] at com.inet.tds.a.a(Unknown Source)
[13:27:36.130] at com.inet.tds.c.new(Unknown Source)
[13:27:36.130] at com.inet.tds.c.executeUpdate(Unknown Source)
[13:27:36.130] at com.caucho.sql.UserPreparedStatement.executeUpdate(UserPreparedStatement.java:117)
[13:27:36.130] at com.caucho.sql.UserPreparedStatement.executeUpdate(UserPreparedStatement.java:117)
[13:27:36.130] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
[13:27:36.130] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1202)
[13:27:36.130] at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:56)
[13:27:36.130] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
[13:27:36.130] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
[13:27:36.130] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
[13:27:36.130] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
[13:27:36.130] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[13:27:36.130] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
[13:27:36.130] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
[13:27:36.130] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
[13:27:36.130] at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
[13:27:36.130] at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:496)
[13:27:36.130] at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:469)
[13:27:36.130] at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:266)
[13:27:36.130] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
[13:27:36.130] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
[13:27:36.130] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
[13:27:36.130] at $Proxy16.setExaminer(Unknown Source)
Name and version of the database you are using:
MS SQL 2000
The generated SQL (show_sql=true):
Hibernate: insert into Examinator_Kurs (Kurs_ID, Termin_ID, Person_ID) values (?, ?, ?)
Debug level Hibernate log excerpt:
[13:40:28.039]Pre-invalidating space [Examinator_Kurs]
[13:40:28.041]Deleting rows of collection: [daisy.shared.kurs.Kurs.examiners#2]
[13:40:28.042]no rows to delete
[13:40:28.042]Updating rows of collection: daisy.shared.kurs.Kurs.examiners#2
[13:40:28.042]done updating rows: 0 updated
[13:40:28.043]Inserting rows of collection: [daisy.shared.kurs.Kurs.examiners#2]
[13:40:28.043]about to open PreparedStatement (open PreparedStatements: 0, globa
lly: 0)
[13:40:28.043]insert into Examinator_Kurs (Kurs_ID, Termin_ID, Person_ID) values
(?, ?, ?)
[13:40:28.230]about to close PreparedStatement (open PreparedStatements: 1, glob
ally: 1)
[13:40:28.245]could not insert collection rows: [daisy.shared.kurs.Kurs.examiner
s#2] [insert into Examinator_Kurs (Kurs_ID, Termin_ID, Person_ID) values (?, ?,
?)]
[13:40:28.245]com.inet.tds.SQLException: Msg 547, Level 16, State 0, Line 1, Sql
state 23000
[13:40:28.245][ITMIRROR]INSERT statement conflicted with COLUMN FOREIGN KEY cons
traint 'FK__Examinato__Perso__56C3C04D'. The conflict occurred in database 'Skar
pKopia', table 'Person', column 'PersonID'.
[13:40:28.245] at com.inet.tds.a.a(Unknown Source)
[13:40:28.245] at com.inet.tds.a.a(Unknown Source)
[13:40:28.245] at com.inet.tds.c.new(Unknown Source)
[13:40:28.245] at com.inet.tds.c.executeUpdate(Unknown Source)
[13:40:28.245] at com.caucho.sql.UserPreparedStatement.executeUpdate(UserPrepar
edStatement.java:117)
[13:40:28.245] at com.caucho.sql.UserPreparedStatement.executeUpdate(UserPrepar
edStatement.java:117)
[13:40:28.245] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingB
atcher.java:23)