简体   繁体   中英

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 auto_increment 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;



@Entity
public class User {

    @Id
    @GenericGenerator(name="generator", strategy="increment")
    @GeneratedValue(generator="generator")
    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) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.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"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 06.05.2016 11:59:26 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.jwt.hibernate.bean.User" table="USER">
        <id name="userId" type="java.lang.Long">
            <column name="USERID" />
            <generator class="assigned" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="USERNAME" />
        </property>
        <property name="password1" type="java.lang.String">
            <column name="PASSWORD1" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
        <property name="phone" type="java.lang.String">
            <column name="PHONE" />
        </property>
        <property name="city" type="java.lang.String">
            <column name="CITY" />
        </property>
    </class>
</hibernate-mapping>

My hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <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="hbm2ddl.auto">create-drop</property>
        <property name="show_sql">true</property>

        <mapping resource="/com/jwt/hibernate/bean/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>

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 auto_increment and I don't know why. I tried a lot of Annotations. other Annotations for Example ManyToMany or ManyToOne works, but nut the @GeneratedValue

Use

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

Instead of following line

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

You can do use this annotations on your getUserId method :

@Id
@GenericGenerator(name = "id_generator", strategy = "increment")
@GeneratedValue(generator = "id_generator")
@Column(name = "id", unique = true, nullable = false)
public Long getUserId() {
      return userId;
}

or you can also do this in your current code just specify @Column on getUserId method

@Column(name = "id", unique = true, nullable = false)
public Long getUserId() {
      return userId;
}

If you will use annotations, this is pretty enough

@Id
@GeneratedValue
@Column(name = "id")
public Long getUserId() {
      return userId;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM