views:

1425

answers:

3

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?

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

This lets Hibernate pick the appropriate strategy based on the underlying database.

hobodave
A: 

Try using @GeneratedValue(strategy=GenerationType.AUTO). That should use the MySQL autonum functionality.

Rob Di Marco
A: 

You might wish to have a look at: http://hibernatepojoge.sourceforge.net/