My environment : Hibernate 5, Java 8, Phpmyadmin in WAMP
Problem: Hibernate creates auto increment id within a table, but the next sequence is given to a different table.
Expected
Table 1 Table 2 1. Hello 1. Foo 2. World 2. Bar Instead it is creating
Table 1 Table 2 1. Hello 2. Foo 3. World 4. Bar Project Structure
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 Structure
certm2m
hibernate_sequence
studentm2m
studentm2m_certm2m





