Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Unknown integral data type for ids

Tags:

java

hibernate

I'm starting with Hibernate and I have an error that I just can't figure out.

I have the following Classes:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractColumn {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    private String name;

//Other stuff
}

Then I have

@Entity
public class DoubleColumn extends AbstractColumn  implements Column {

    @ElementCollection
    private Map<Double,String> isNA;
    private double min=0;
    private double max=0;
    @ElementCollection
    private List<Double> data;
    // a lot of stuff
}

And finally:

@Entity
public class DataFrame {
    @OneToMany(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private List<AbstractColumn> data;
    private String name;
    @Id
    @GeneratedValue
    private String id;
    @ElementCollection
    private Map<String,Integer> colIndex;
//more stuff
}

The error I'm getting is:

Exception in thread "main" org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String
    at org.hibernate.id.IdentifierGeneratorHelper.getIntegralDataTypeHolder(IdentifierGeneratorHelper.java:224)
    at org.hibernate.id.enhanced.SequenceStructure$1.getNextValue(SequenceStructure.java:98)
    at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:432)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:675)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:667)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:662)
    at Main.main(Main.java:285)

The only hint that the error throws is that the error is in the main class here:

DoubleColumn c1 = new DoubleColumn("Datos varios");
        c1.addData(12);
        c1.addData(11);
        c1.addData(131);
        c1.addData(121);
        c1.addData(151);
        c1.addData(116);


        DataFrame datosHibernate = new DataFrame("Dataframe Hibernate");
        datosHibernate.addColumn(c1);


        Configuration hibernateConfig = new Configuration();
        SessionFactory sessionFactory = hibernateConfig.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(datosHibernate);
        session.getTransaction().commit();
        session.disconnect();
        session.close();
        System.exit(0);

The ids are Strings, and I have them annotated as @GeneratedValue (I think I do not need to initialize them myself). The relation @OneToMany have the cascade annotation so it should be mapped correctly.

I've tried the code WITHOUT the session.save line and doesn't throw errors, so is not a problem in the code per se, it has to be something with Hibernate.

My configuration file is:

<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:h2:E:/bd;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.username">user</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

        <mapping class="com.dataframe.estructuras.DataFrame"></mapping>
        <mapping class="com.dataframe.estructuras.column.AbstractColumn"/>
        <mapping class="com.dataframe.estructuras.column.types.DoubleColumn"/>
    </session-factory>
</hibernate-configuration>

It creates the database file and I see all the SQL code generated, tables and all. Is when it tries to store the info when the thing breaks.

like image 924
Caronte Hades Avatar asked Oct 21 '16 13:10

Caronte Hades


Video Answer


1 Answers

If you want String to be database key you need to use UUID to automatically generate key. Something like this:

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String myId;
like image 141
user2026753 Avatar answered Oct 01 '22 23:10

user2026753