Hibernate 正在为表生成自动增量交替 id
Hibernate is generating auto increment alternating id for tables
我的环境:Hibernate 5
、Java 8
、Phpmyadmin in WAMP
问题:Hibernate 在 table 中创建自动递增 id,但下一个序列被赋予不同的 table。
预计
Table 1 Table 2
1. Hello 1. Foo
2. World 2. Bar
相反,它正在创建
Table 1 Table 2
1. Hello 2. Foo
3. World 4. Bar
项目结构
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ansarihibernate</property>
<property name="connection.username">localuser</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- Many To Many classes -->
<mapping class="org.ansari.hibernate.manytomany.StudentM2M"/>
<mapping class="org.ansari.hibernate.manytomany.StudentM2MCertificates"/>
</session-factory>
MainM2M.java
package org.ansari.hibernate.manytomany;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class MainM2M {
public static void main(String[] args) {
StudentM2MCertificates st1 = new StudentM2MCertificates();
st1.setM2m_cert_det("Oracle Cert");
StudentM2MCertificates st2 = new StudentM2MCertificates();
st2.setM2m_cert_det("Big Data Cert");
StudentM2M s1 = new StudentM2M();
s1.setM2m_stu_name("Ansari");
s1.getSetM2MCert().add(st1);
StudentM2M s2 = new StudentM2M();
s2.setM2m_stu_name("Mohammed");
s2.getSetM2MCert().add(st2);
SessionFactory sessFac = new Configuration().configure().buildSessionFactory();
Session session = sessFac.openSession();
session.beginTransaction();
session.save(s1);
session.save(s2);
session.getTransaction().commit();
session.close();
sessFac.close();
}
}
StudentM2M.java
package org.ansari.hibernate.manytomany;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="STUDENTM2M")
public class StudentM2M {
@Id
@GeneratedValue
private int m2m_id;
private String m2m_stu_name;
@ManyToMany(cascade=CascadeType.ALL)
private Set<StudentM2MCertificates> setM2MCert = new HashSet<StudentM2MCertificates>(0);
public Set<StudentM2MCertificates> getSetM2MCert() {
return setM2MCert;
}
public void setSetM2MCert(Set<StudentM2MCertificates> setM2MCert) {
this.setM2MCert = setM2MCert;
}
public int getM2m_id() {
return m2m_id;
}
public void setM2m_id(int m2m_id) {
this.m2m_id = m2m_id;
}
public String getM2m_stu_name() {
return m2m_stu_name;
}
public void setM2m_stu_name(String m2m_stu_name) {
this.m2m_stu_name = m2m_stu_name;
}
}
StudentM2MCertificates
package org.ansari.hibernate.manytomany;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CERTM2M")
public class StudentM2MCertificates {
@Id
@GeneratedValue
private int m2m_cert_id;
private String m2m_cert_det;
public int getM2m_cert_id() {
return m2m_cert_id;
}
public void setM2m_cert_id(int m2m_cert_id) {
this.m2m_cert_id = m2m_cert_id;
}
public String getM2m_cert_det() {
return m2m_cert_det;
}
public void setM2m_cert_det(String m2m_cert_det) {
this.m2m_cert_det = m2m_cert_det;
}
}
Table结构
certm2m
hibernate_sequence
studentm2m
studentm2m_certm2m
您还没有为您的任何实体指定任何生成器。因此,Hibernate 为您的数据库使用默认序列,即使用单个序列:hibernate_sequence 来生成实体的 ID。
用类似
的方式注释您的学生 ID 字段
@SequenceGenerator(name = "studentGenerator", sequenceName = "STUDENT_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentGenerator")
并且它将为 Student 实体使用专用序列 STUDENT_SEQUENCE。对其他实体执行相同操作(使用不同的生成器和序列名称)。
我的环境:Hibernate 5
、Java 8
、Phpmyadmin in WAMP
问题:Hibernate 在 table 中创建自动递增 id,但下一个序列被赋予不同的 table。
预计
Table 1 Table 2
1. Hello 1. Foo
2. World 2. Bar
相反,它正在创建
Table 1 Table 2
1. Hello 2. Foo
3. World 4. Bar
项目结构
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ansarihibernate</property>
<property name="connection.username">localuser</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- Many To Many classes -->
<mapping class="org.ansari.hibernate.manytomany.StudentM2M"/>
<mapping class="org.ansari.hibernate.manytomany.StudentM2MCertificates"/>
</session-factory>
MainM2M.java
package org.ansari.hibernate.manytomany;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class MainM2M {
public static void main(String[] args) {
StudentM2MCertificates st1 = new StudentM2MCertificates();
st1.setM2m_cert_det("Oracle Cert");
StudentM2MCertificates st2 = new StudentM2MCertificates();
st2.setM2m_cert_det("Big Data Cert");
StudentM2M s1 = new StudentM2M();
s1.setM2m_stu_name("Ansari");
s1.getSetM2MCert().add(st1);
StudentM2M s2 = new StudentM2M();
s2.setM2m_stu_name("Mohammed");
s2.getSetM2MCert().add(st2);
SessionFactory sessFac = new Configuration().configure().buildSessionFactory();
Session session = sessFac.openSession();
session.beginTransaction();
session.save(s1);
session.save(s2);
session.getTransaction().commit();
session.close();
sessFac.close();
}
}
StudentM2M.java
package org.ansari.hibernate.manytomany;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="STUDENTM2M")
public class StudentM2M {
@Id
@GeneratedValue
private int m2m_id;
private String m2m_stu_name;
@ManyToMany(cascade=CascadeType.ALL)
private Set<StudentM2MCertificates> setM2MCert = new HashSet<StudentM2MCertificates>(0);
public Set<StudentM2MCertificates> getSetM2MCert() {
return setM2MCert;
}
public void setSetM2MCert(Set<StudentM2MCertificates> setM2MCert) {
this.setM2MCert = setM2MCert;
}
public int getM2m_id() {
return m2m_id;
}
public void setM2m_id(int m2m_id) {
this.m2m_id = m2m_id;
}
public String getM2m_stu_name() {
return m2m_stu_name;
}
public void setM2m_stu_name(String m2m_stu_name) {
this.m2m_stu_name = m2m_stu_name;
}
}
StudentM2MCertificates
package org.ansari.hibernate.manytomany;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CERTM2M")
public class StudentM2MCertificates {
@Id
@GeneratedValue
private int m2m_cert_id;
private String m2m_cert_det;
public int getM2m_cert_id() {
return m2m_cert_id;
}
public void setM2m_cert_id(int m2m_cert_id) {
this.m2m_cert_id = m2m_cert_id;
}
public String getM2m_cert_det() {
return m2m_cert_det;
}
public void setM2m_cert_det(String m2m_cert_det) {
this.m2m_cert_det = m2m_cert_det;
}
}
Table结构
certm2m
hibernate_sequence
studentm2m
studentm2m_certm2m
您还没有为您的任何实体指定任何生成器。因此,Hibernate 为您的数据库使用默认序列,即使用单个序列:hibernate_sequence 来生成实体的 ID。
用类似
的方式注释您的学生 ID 字段@SequenceGenerator(name = "studentGenerator", sequenceName = "STUDENT_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentGenerator")
并且它将为 Student 实体使用专用序列 STUDENT_SEQUENCE。对其他实体执行相同操作(使用不同的生成器和序列名称)。