Trying to use a custom id generator with hibernate, getting a mapping error

5,160 views
Skip to first unread message

Barclay Dunn

unread,
Jan 23, 2014, 5:22:33 PM1/23/14
to dropwiz...@googlegroups.com

I need a custom id generator that saves the last used id rather than the next available, so I created a custom generator extending TableGenerator. Documentation for this is available at http://docs.jboss.org/hibernate/core/4.2/manual/en-US/html_single/#mapping-declaration-id-enhanced and I also followed a suggestion found in StackOverflow at http://stackoverflow.com/a/10648572/187423.

I've determined in debug mode that my custom generator is not getting added to org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.generatorStrategyToClassNameMap, so it doesn't know how to interpret my generator. (It also seems that I should be using a handle rather than a class name, but again - not sure how to get this into the map.)

Using Dropwizard complicates things. I'm not sure how or where I need to let DW know about my custom generator, but I've tried adding my class to the HibernateBundle, to the run() method in my DW service, to the injector, etc.

Here's where I use my generator in the class:

public class MexpFeedError {
    private int mexpFeedErrorId;

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE, generator = "MEXPFEEDERROR_GEN")
    @GenericGenerator(name = "MEXPFEEDERROR_GEN", strategy = "com.pronto.util.ProntoPersistJustUsedGenerator",
            parameters = {
                    @org.hibernate.annotations.Parameter(name = "table_name", value = "sequence"),
                    @org.hibernate.annotations.Parameter(name = "segment_value", value = "seq_mexpfeederrorid"),
                    @org.hibernate.annotations.Parameter(name = "value_column_name", value = "sequence_value"),
                    @org.hibernate.annotations.Parameter(name = "segment_column_name", value = "sequence_name") })
    public int getMexpFeedErrorId() {
        return mexpFeedErrorId;
    }
    ...
}

Here's my custom generator:

package com.pronto.util.hibernate;

import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.enhanced.TableGenerator;

import java.io.Serializable;

@SuppressWarnings("UnusedDeclaration")
public class ProntoPersistJustUsedGenerator extends TableGenerator {

    @Override
    public synchronized Serializable generate(SessionImplementor session, Object obj) {
        return (Long) super.generate(session, obj) + 1;
    }
}

Here's the stack trace:

Exception in thread "main" org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.pronto.mpds.entity.MexpFeedError]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:123)
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:195)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769)
    at com.yammer.dropwizard.hibernate.SessionFactoryFactory.buildSessionFactory(SessionFactoryFactory.java:77)
    at com.yammer.dropwizard.hibernate.SessionFactoryFactory.build(SessionFactoryFactory.java:35)
    at com.yammer.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.java:38)
    at com.yammer.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.java:13)
    at com.yammer.dropwizard.config.Bootstrap.runWithBundles(Bootstrap.java:77)
    at com.yammer.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:37)
    at com.yammer.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:58)
    at com.yammer.dropwizard.cli.Cli.run(Cli.java:53)
    at com.yammer.dropwizard.Service.run(Service.java:61)
    at com.pronto.mpds.service.DropwizardMPDSService.main(DropwizardMPDSService.java:42)
    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:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.hibernate.MappingException: Could not interpret id generator strategy [com.pronto.util.ProntoSequenceStyleGenerator]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:140)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:114)
    ... 18 more

Hope there's a (drop)wizard out there with some useful ideas for me. Thanks.

Barclay Dunn

unread,
Jan 24, 2014, 10:43:46 AM1/24/14
to dropwiz...@googlegroups.com
It was such an easy fix - I was overcomplicating things by thinking the issue related somehow to passing my generator into the hibernateBundle. But in fact it was just a misidentification of the generator. I specified the wrong package name. I had "com.pronto.util.ProntoPersistJustUsedGenerator" when it should have been "com.pronto.util.hibernate.ProntoPersistJustUsedGenerator". Thanks to an eagle-eyed Stack Overflow user.

Barclay
Exception in thread "main" org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.pronto.mpds.entity.MexpFeedError]
java:53)
<
span class="pln" style="background-color: transparent;">
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.hibernate.MappingException: Could not interpret id generator strategy [com.pronto.util.ProntoSequenceStyleGenerator]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:140)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:114)
    ... 18 more

Hope there's a (drop)wizard out there with some useful ideas for me. Thanks.

--
You received this message because you are subscribed to a topic in the Google Groups "dropwizard-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dropwizard-user/SxfDQLSVwqs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages