5

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?

Mat
  • 202,337
  • 40
  • 393
  • 406

5 Answers5

0

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>
0

I face the same problem. I think when you don't have any key in the table then it generates two classes. Otherwise, if you have any key in the table then it generates only one.

At least for me after adding a primary key in the table it is generating only one Class representing the table. If I remove the primary key then it generates two classes.

Old thread, but I thought useful for somebody.

shilovk
  • 11,718
  • 17
  • 75
  • 74
Govind
  • 311
  • 1
  • 12
0

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
  • 29,950
  • 9
  • 48
  • 62
0

You must go in yout db and check if you had already set a field as Primary key, then the hibernate reverse engineering file, don't make multiple class anymore.

Dwhitz
  • 1,250
  • 7
  • 26
  • 38
0

I had a similar issue running the tool in Eclipse against a teradata instance. I had several views to reverse, and they were in a schema. I was getting the AcxiomDataId class generated with this:

<table catalog=".*" schema="U01TKE_GRPR_RADMT_VW" name="F_ACXM_MBR" class="AcxiomData">
    <primary-key>
        <generator class="increment"></generator>
        <key-column name="MBR_UNIQ_KEY" property="memberUniqKey" />
    </primary-key>
</table>

But removing the schema and catalog attributes from the table element, I did not get the AcxiomDataId class:

<table name="F_ACXM_MBR" class="AcxiomData">
    <primary-key>
        <generator class="increment"></generator>
        <key-column name="MBR_UNIQ_KEY" property="memberUniqKey" />
    </primary-key>
</table>

No idea why that is.

Alper Akture
  • 2,445
  • 1
  • 30
  • 44