2
votes

I am new to Hibernate and I seem to be having trouble getting Hibernate to correctly reverse engineer a MySQL database (eg: https://launchpad.net/test-db/) into Java classes.

In the employees database schema there are several tables:

  • employees (primary key emp_no)
  • department (primary key dept_no)
  • salaries (composite keys emp_no and from_date)
  • dept_emp (composite keys dept_no and emp_no)

The problem is that when I reverse engineer the table (config and reveng files below), instead of creating the above 4 java classes, Hibernate is giving me 6 Java classes with extra ones bolded:

  • Employees
  • Departments
  • Salaries
  • SalariesId
  • DeptEmp
  • DeptEmpId

As far as I can tell, for the tables with composite primary keys, Hibernate is creating a new "classnameID" class that contains the unique combination of the two composite keys. Instead, it would be awesome to just have, for example, the DeptEmp class with a dept_no and a set/list of emp_no's that are associated with that particular department.

Any ideas how I can get Hibernate not to do this? I've been trying lots of things in the .reveng file, and nothing seems to be working.

Reverse Engineering File:

<hibernate-reverse-engineering>
   <table-filter match-catalog="employees" match-name="departments" />
   <table-filter match-catalog="employees" match-name="dept_emp" />
   <table-filter match-catalog="employees" match-name="employees" />
   <table-filter match-catalog="employees" match-name="salaries" />

   <table schema="employees" name="departments">
      <primary-key>
         <key-column name="dept_no" />
      </primary-key>
   </table>   
   <table schema="employees" name="employees">
      <primary-key>
         <key-column name="emp_no" />
      </primary-key>
   </table>
   <table schema="employees" name="salaries">
      <primary-key>
         <key-column name="emp_no" />
         <key-column name="from_date" />
      </primary-key>
   </table>
   <table schema="employees" name="dept_emp">
      <primary-key>
         <key-column name="emp_no" />
         <key-column name="dept_no" />
      </primary-key>
   </table>
</hibernate-reverse-engineering>

Configuration File:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/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/employees</property>
      <property name="hibernate.connection.username">****</property>
      <property name="hibernate.connection.password">****</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="connection.pool_size">20</property>
      <property name="current_session_context_class">thread</property>
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      <property name="show_sql">true</property>
      <property name="format_sql">true</property>
      <property name="use_sql_comments">true</property>
    </session-factory>
</hibernate-configuration>

Thanks!

1
Did you manage to make this work? - Gonçalo Cardoso
The problem still exists. Anyone got an idea? - MavidDeyers

1 Answers

0
votes

This behavour is caused by having multiple columns as ID in a single table, that is why Departments table doesn´t have the aditional IDClass, and Salaries does.

So, how do you get around this? In your reveng file set a unique ID for each table. This likely will cause some issues in your code if you do not treat all the primary keys from a table correctly, so be cautious.

Also you could have this extra classes and just treat them as a int or a String, you just initialize them properly and set them in your entities. Hopefully I could help you as I had this issue and it was very strenge for me.