-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: @CollectionId generator="native" fails
PostPosted: Wed Feb 11, 2009 6:36 pm 
Newbie

Joined: Fri Aug 05, 2005 2:56 pm
Posts: 11
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)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 5:43 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Have you tried changing the type to long?


--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 12:46 pm 
Newbie

Joined: Fri Aug 05, 2005 2:56 pm
Posts: 11
I tried 'long' as well and I receive the same result.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 10:34 am 
Newbie

Joined: Fri Aug 05, 2005 2:56 pm
Posts: 11
Is this a bug in Hibernate???

Any ideas? Workarounds? Any help at this point is much appreciated!

-Kyle


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 2:07 pm 
Newbie

Joined: Fri Feb 13, 2009 1:46 pm
Posts: 9
I am encountering the same problem with the following code.

Code:
@CollectionOfElements
@JoinTable(name = "contact",
                 joinColumns = @JoinColumn(name = "organization_id"))
@CollectionId(
        columns = @Column(name = "contact_id"),
        type = @Type(type = "integer"),
        generator = "identity"
)
private Collection<Contact> contacts = new ArrayList<Contact>();


I don't know if this is a bug in Hibernate, but you should be able to work around the problem by converting your ColumnSB class to an entity. You can then map ColumnSB as a child of DashboardSB.


Top
 Profile  
 
 Post subject: Re: @CollectionId generator="native" fails
PostPosted: Fri Dec 17, 2010 6:33 am 
Newbie

Joined: Tue Nov 02, 2010 10:07 am
Posts: 1
Exception in thread "main" java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper$2 cannot be cast to java.lang.Integer
at org.hibernate.type.IntegerType.set(IntegerType.java:64)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:154)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeIdentifier(AbstractCollectionPersister.java:868)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1199)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:183)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.assignment.collectionid.App.main(App.java:30)













I am also getting this type of error in following piece of code...Please Help..

@ManyToMany


@CollectionId(
columns = @Column(name="ITEM_SHIPMENT_ID"),
type=@org.hibernate.annotations.Type(type = "integer"),
generator="identity"
)
@JoinTable(
name ="item_shipment_many",
joinColumns ={@JoinColumn(name="SHIPMENT_ID",nullable=false)},
inverseJoinColumns ={@JoinColumn(name="ITEM_ID")}
)
private Collection<ItemCollectionsecond> itemc = new ArrayList<ItemCollectionsecond>() ;

Please Reply At prafful.namdev18@gmail.com if ther is any solution.


Top
 Profile  
 
 Post subject: Re: @CollectionId generator="native" fails
PostPosted: Tue Nov 04, 2014 2:45 am 
Newbie

Joined: Tue Nov 04, 2014 2:32 am
Posts: 1
@ManyToMany
@GenericGenerator(name="hilo-gen", strategy="increment")
@CollectionId(columns={@Column(name="sof_supp_cust_mapp_id_pk")}, generator="hilo-gen", type=@Type(type="int"))
@JoinTable(name="sof_supp_cust_mapp_deta", joinColumns={@JoinColumn(name="sof_serv_id_pk")},
inverseJoinColumns={@JoinColumn(name="mas_cust_info_cust_id_fk")})
private List<CustomerMasterInfo> listOfSupportToCustomers = new ArrayList<CustomerMasterInfo>();


above snippet is not working in my app cluster....Two of my app servers accessing same DB and trying to inserting data....

while saving dashboard object giving org.hibernate.exception.ConstraintViolationException: Duplicate entry '228' for key 'PRIMARY' error..

can any one help me please....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.