Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The equivalent of <generator class="native"></generator> using MySQL and Hibernate3 annotations

Starting a new project I'd like to use Hibernate annotations with MySQL instead of the configuration files I've used so far. And I can't seem to find the equivalent of:

    <id name="id" type="long" >
        <generator class="native"></generator>
    </id>

I tried using:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
private long id;

but got:

org.hibernate.AnnotationException: Unknown Id.generator: native

or:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

Give me:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: PROCEDURE projectName.identity does not exist

Does anyone successfully deployed MySQL and Hibernate3 annotations for automatically generating ids?

like image 231
Guy Gavriely Avatar asked Jul 31 '09 13:07

Guy Gavriely


People also ask

What does generator class equal to Native mean?

Native means Your generator will use identity or sequence columns according to what your current database support.

What is the use of generator class in hibernate?

A generator class is used to generate an ID for an object, which is going to be inserted in the database as a primary key. The ID is a unique identifier for the objects of the persistent class. We can use any generator classes in our application as per our requirements.

What is sequence generator in hibernate?

SEQUENCE Generation. To use a sequence-based id, Hibernate provides the SequenceStyleGenerator class. This generator uses sequences if our database supports them. It switches to table generation if they aren't supported.


1 Answers

Prior to version 5.0, using the strategy AUTO was the equivalent of using native in a mapping. This used the LegacyFallbackInterpreter:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    return id;
}

Since Hibernate 5.0, the default interpreter is the FallbackInterpeter which will either use a SEQUENCE generator or TABLE generator depending on the underlying database.

To use the LegacyFallbackInterpreter, set hibernate.id.new_generator_mappings to false.

like image 157
hobodave Avatar answered Sep 17 '22 16:09

hobodave