Hibernate version: 3.2.6
MySQL 5.0.4
I am attempting to persist a Dashboard Entity that contains a collection of column 'value-type' objects but the @CollectionId generator="native" fails. I have tried various generator types all with the same outcome. Is this a bug?
Here is the code and the stack trace. I have also included a simple JUnit test I am running to reproduce the issue::
Code:
package com.sandbox.model;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.Type;
import com.thesearchagency.model.base.BaseDO;
@Entity
@Table(name = "sb_dashboard")
public class DashboardSB extends BaseDO
{
private static final long serialVersionUID = 20090123001L;
@Id
@Column(name = "dashboard_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer theId;
@Column(name = "name")
private String theName;
@CollectionOfElements
@JoinTable(name = "sb_dashboard_column", joinColumns = @JoinColumn(name = "dashboard_id"))
@CollectionId(columns = @Column(name = "column_id"), type = @Type(type = "integer"), generator = "native")
private Collection<ColumnSB> columns = new ArrayList<ColumnSB>();
/**
* @return the id
*/
public Integer getId()
{
return theId;
}
/**
* @param anId the id to set
*/
public void setId(Integer anId)
{
theId = anId;
}
/**
* @return the name
*/
public String getName()
{
return theName;
}
/**
* @param anName the name to set
*/
public void setName(String anName)
{
theName = anName;
}
/**
* @return the columns
*/
public Collection<ColumnSB> getColumns()
{
return columns;
}
/**
* @param anColumns the columns to set
*/
public void setColumns(Collection<ColumnSB> anColumns)
{
columns = anColumns;
}
/**
* String representation of a DashboardDO object
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
buff.append(this.getClass().getSimpleName() + "\n");
buff.append("\tId :: " + this.theId + "\n");
buff.append("\tName :: " + this.theName + "\n");
return buff.toString();
}
}
Code:
package com.sandbox.model;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.annotations.Parent;
import com.thesearchagency.model.base.BaseDO;
/**
* @author kbober
*/
@Embeddable
public class ColumnSB extends BaseDO
{
private static final long serialVersionUID = 20090123001L;
@Parent
private DashboardSB dashboard;
@Column(name = "column_index")
private Integer theIndex;
/**
* @return the dashboard
*/
public DashboardSB getDashboard()
{
return dashboard;
}
/**
* @param anDashboard the dashboard to set
*/
public void setDashboard(DashboardSB anDashboard)
{
dashboard = anDashboard;
}
/**
* @return the index
*/
public Integer getIndex()
{
return theIndex;
}
/**
* @param anIndex the index to set
*/
public void setIndex(Integer anIndex)
{
theIndex = anIndex;
}
/**
* String representation of a DashboardColumnDO object
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
buff.append(this.getClass().getSimpleName() + "\n");
buff.append("\tIndex :: " + this.theIndex + "\n");
return buff.toString();
}
}
Code:
package com.sandbox.model;
import org.hibernate.Transaction;
import org.junit.Test;
import com.thesearchagency.dao.dashboard.util.HibernateUtil;
/**
* @author kbober
*
*/
public class SandboxTest
{
@Test
public void simpleTest() {
DashboardSB dashboard = new DashboardSB();
dashboard.setName("Hello World");
ColumnSB column1 = new ColumnSB();
column1.setIndex(0);
column1.setDashboard(dashboard);
ColumnSB column2 = new ColumnSB();
column2.setIndex(1);
column2.setDashboard(dashboard);
dashboard.getColumns().add(column1);
dashboard.getColumns().add(column2);
Transaction tx = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
Integer dashboardId = (Integer)HibernateUtil.getSessionFactory().getCurrentSession().save(dashboard);
tx.commit();
tx = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
DashboardSB anotherDashboard = (DashboardSB)HibernateUtil.getSessionFactory().getCurrentSession().get(DashboardSB.class, dashboardId);
tx.commit();
}
}
Stack trace ::
java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorFactory$2
at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeIdentifier(AbstractCollectionPersister.java:807)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1138)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:39)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sandbox.model.SandboxTest.simpleTest(SandboxTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)