views:

1908

answers:

2

I have a table in my MYSQL database which does not have a primary key, but has a unique key on two columns. When using MyEclipse's Hibernate reverse engineer tool to create a mapping for that table, it generates two classes, one for named after the table itself, and one with an "Id" suffix. It seems most of the useful methods ended up in the Id class, so it seems that is the one you would instantiate and save to persist data. I can appreciate the fact that the Id class is created in order to represent a unique row in the table / mapped object, but what is the use of splitting out this into two classes, and what, then is the use of the non-Id-suffixed class?

My colleague argues you can accomplish the same with just one class and scoffs at using the reverse engineering for these tables which don't have a primary key. I, on the other hand, assume MyEclipse developers are much smarter than me and that there is a really good reason to do it this way. Is there?

A: 

You assume too much my friend. Those tools are not actually from the MyEclipse team, its from the Hibernate Tools project (JBoss, the developers of Hibernate).

This is a programmatic tool that can't guess everything. It is pretty good for simple stuff well anotated, but sometimes, it will not generate exactly what you need.

The id class is needed usually to represent a composite primary key (a key using multiple attributes). It uses the component classes Hibernate concept.

It's also possible to tweak the generator options a bit.

In your case, it would be best to do as your colleagues say. Create your own entity classes.

Loki
A: 

In your reverse engineer file:

   <table schema="public" name="yourtable">
         <primary-key>
                <!-- generator may not be necessary for mysql -->
          <generator class="increment"></generator> 
          <key-column name="column_name_of_primary_key" />
         </primary-key>
        </table>