Niklas Ehrenholz Niklas Ehrenholz - 2 years ago 409
Java Question

Hibernate auto_increment MySQL

I am using Eclipse, Xammp (tomcat and MySQL DB) and Hibernate.

this all works good, but i can't create that the ID from the Entity will be

in the Database

My Entity:

package com.jwt.hibernate.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;

public class User {

@GenericGenerator(name="generator", strategy="increment")
private Long userId;
private String userName;
private String password1;
private String email;
private String phone;
private String city;

public Long getUserId() {
return userId;

public void setUserId(Long userId) {
this.userId = userId;

public String getUserName() {
return userName;

public void setUserName(String userName) {
this.userName = userName;

public String getPassword1() {
return password1;

public void setPassword1(String password1) {
this.password1 = password1;

public String getEmail() {
return email;

public void setEmail(String email) { = email;

public String getPhone() {
return phone;

public void setPhone(String phone) { = phone;

public String getCity() {
return city;

public void setCity(String city) { = city;

I create a hbm.xml for this Entity with a Plugin from Hibernate and use
Hibernate XML Mapping file(hbm.xml)
Created hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<!-- Generated 06.05.2016 11:59:26 by Hibernate Tools 3.5.0.Final -->
<class name="com.jwt.hibernate.bean.User" table="USER">
<id name="userId" type="java.lang.Long">
<column name="USERID" />
<generator class="assigned" />
<property name="userName" type="java.lang.String">
<column name="USERNAME" />
<property name="password1" type="java.lang.String">
<column name="PASSWORD1" />
<property name="email" type="java.lang.String">
<column name="EMAIL" />
<property name="phone" type="java.lang.String">
<column name="PHONE" />
<property name="city" type="java.lang.String">
<column name="CITY" />

My hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="">create-drop</property>
<property name="show_sql">true</property>

<mapping resource="/com/jwt/hibernate/bean/User.hbm.xml" />

If i start my Code, the Console do:

Hibernate: drop table if exists USER
Hibernate: create table USER (USERID bigint not null, USERNAME varchar(255), PASSWORD1 varchar(255), EMAIL varchar(255), PHONE varchar(255), CITY varchar(255), primary key (USERID))

Every thing is fine but my ID isn't
and i don't now why.
I tryed a lot of Anotations.
other Anotations for Example
works, but nut the

have no more ideas.

Answer Source


    <id name="userId" type="java.lang.Long">
        <column name="USERID" />
        <generator class="native" />

Instead of following line

    <id name="userId" type="java.lang.Long">
        <column name="USERID" />
        <generator class="assigned" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download