gpt4 book ai didi

java - Hibernate auto_increment MySQL

转载 作者:行者123 更新时间:2023-11-29 02:17:40 30 4
gpt4 key购买 nike

我正在使用 Eclipse、Xammp(tomcat 和 MySQL DB)和 Hibernate。

这一切都很好,但我无法创建实体的 ID 在数据库中是 auto_increment

我的实体:

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

我使用来自 Hibernate 的插件为此实体创建了一个 hbm.xml 并使用 Hibernate XML 映射文件 (hbm.xml):创建的 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>

我的 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>

如果我开始我的代码,控制台会:

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))

一切正常,但我的 ID 不是 auto_increment,我不知道为什么。我尝试了很多注释。示例 ManyToManyManyToOne 的其他注释有效,但是 @GeneratedValue

最佳答案

使用

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

代替下一行

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

关于java - Hibernate auto_increment MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37070968/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com