Hibernate正在为表生成自动增量交替ID [英] Hibernate is generating auto increment alternating id for tables
问题描述
我的环境:Hibernate 5
,Java 8
,Phpmyadmin in WAMP
问题:Hibernate在一个表内创建自动增量ID,但是下一个序列被赋予另一个表.
Problem: Hibernate creates auto increment id within a table, but the next sequence is given to a different 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;
}
}
学生M2MC证书
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;
}
}
表结构
certm2m
hibernate_sequence
studentm2m
studentm2m_certm2m
推荐答案
您尚未为任何实体指定任何生成器.因此,Hibernate为您的数据库使用默认值,即使用单个序列hibernate_sequence来生成实体的ID.
You haven't specified any generator for any of your entities. So Hibernate uses the default one for your database, which consists in using single sequence: hibernate_sequence, to generate the IDs of the entities.
用诸如此类的注释您的学生证字段
Annotate your Student id field with something like
@SequenceGenerator(name = "studentGenerator", sequenceName = "STUDENT_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "studentGenerator")
,它将为学生实体使用专用序列STUDENT_SEQUENCE.对其他实体执行相同的操作(使用不同的生成器和序列名称).
and it will use a dedicated sequence, STUDENT_SEQUENCE, for the Student entity. Do the same (with a different generator and sequence name) for the other entities.
这篇关于Hibernate正在为表生成自动增量交替ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!