I'm getting a ConstraintViolationException when I try to add an inheritance-mapped object to my database.
Hibernate version 3.1.3
Spring 1.2.8
Apache Derby 10.1
Mapping:
Code:
<hibernate-mapping>
<class name="org.something.security.Authentication" table="MyAuthentication">
<id name="id" type="int" column="authenticationId" unsaved-value="-1">
<generator class="native"/>
</id>
<joined-subclass name="org.something.security.PasswordAuthentication" table="MyPasswordAuthentication">
<key column="authenticationId"/>
<property name="username" type="string" column="username" length="30" not-null="true"/>
<property name="password" type="string" column="password" length="30" not-null="true"/>
</joined-subclass>
</class>
</hibernate-mapping>
Data classes:
Code:
public abstract class Authentication
{
private int id;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}
public class PasswordAuthentication extends Authentication
{
public static final int USERNAME_LENGTH = 30;
public static final int PASSWORD_LENGTH = 30;
private String username;
private String password;
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
}
Code where it dies:
Code:
In class ApplicationInitializer:
PasswordAuthentication auth = new PasswordAuthentication();
auth.setUsername("test");
auth.setPassword("test");
securityDAO.create(auth); // Line 56
In class SecurityDAO:
public void create(Authentication auth)
{
getHibernateTemplate().save(auth);
}
Exception:
Quote:
org.hibernate.exception.ConstraintViolationException: could not insert: [org.something.data.security.PasswordAuthentication]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2078)
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)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:500)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:473)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:267)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy1.create(Unknown Source)
at org.something.ApplicationInitializer.initialize(ApplicationInitializer.java:56)
at org.something.servlet.ApplicationServlet.init(ApplicationServlet.java:44)
at javax.servlet.GenericServlet.init(GenericServlet.java:168)
at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:383)
at org.mortbay.jetty.servlet.ServletHolder.start(ServletHolder.java:243)
at org.mortbay.jetty.servlet.ServletHandler.initializeServlets(ServletHandler.java:446)
at org.mortbay.jetty.servlet.WebApplicationHandler.initializeServlets(WebApplicationHandler.java:321)
at org.mortbay.jetty.servlet.WebApplicationContext.doStart(WebApplicationContext.java:509)
at org.mortbay.util.Container.start(Container.java:72)
at org.mortbay.http.HttpServer.doStart(HttpServer.java:708)
at org.mortbay.util.Container.start(Container.java:72)
at com.iw.plugins.jettyrunner.PluginRunner.launch(PluginRunner.java:282)
at com.iw.plugins.jettyrunner.PluginRunner.launch(PluginRunner.java:104)
at com.iw.plugins.jettyrunner.PluginRunner.main(PluginRunner.java:75)
Caused by: SQL Exception: INSERT on table 'MYAUTHENTICATION' caused a violation of foreign key constraint 'FK2EE15EA8E4AC91FB' for key (163840). The statement has been rolled back.
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2062)
... 32 more
As far as I can tell, this is the same setup as specified in
http://www.hibernate.org/hib_docs/v3/reference/en/html/inheritance.html...
What am I doing wrong?