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!