Menu

#430 Hibernate POJO Generator Needs Option To Turn Off ID Generat

closed-works-for-me
nobody
5
2010-05-15
2010-04-15
Ghosty
No

This affects Opentaps 1.4

There needs to be a better way to disable the setting of OpentapsIdentifierGenerator as the GenericGenerator for hibernate.

Currently this is hacked together in the BaseEntity.ftl template. It checks to see if the primary key is a composite key and if it is just a single field key it renders the annotation to set the OpentapsIdentifierGenerator as the GenericGenerator in the template.

This coupled with the need to set a primary key in the entitymodel.xml files results in some situations where this causes problems of ids being overwritten incorrectly.

An example of this is the two entities DataResource and ElectronicText

Since there has to be a primary key set in the entitymodel.xml you will see that the primary key is set on the "dataResourceId" field of the ElectronicText model. Due to this, by default this causes ElectronicText to attempt to generate and set an id for it's primary key "dataResourceId" when in fact it should just be the value from the DataResource table.

We run into this problem when creating records like so:

DataResource dataResource = new DataResource();
dataResource.setDataResourceTypeId(
DATARESOURCETYPE_ELECTRONIC_TEXT);
dataResource.setDataTemplateTypeId(DATATEMPLATETYPE_FTL);
dataResource.setMimeTypeId(MIMETYPE_TEXTHTML);
String dataResourceId = (String) session.save(dataResource);
dataResource.setDataResourceId(dataResourceId);

ElectronicText electronicText = new ElectronicText();
electronicText.setDataResourceId(dataResourceId);
electronicText.setTextData(landingPage.getPageFTL());

session.save(electronicText);

The electronicText object will hold the correct id at this point, however once we hit the hibernate saveOrUpdate method which then catches the transient entity and runs the GenericGenerator it replaces the id with the one generated which is incorrect.

Discussion

  • Si Chen

    Si Chen - 2010-04-20

    What do you think should be the solution? Should we make hibernate check if an ID has already been set, and if it has, then make sure that this ID is used?

     
  • spark sun

    spark sun - 2010-04-21

    I added your case in r15437, I think hibernate generator will not try to regenerate an pk for the entity with not-null value already.

     
  • Ghosty

    Ghosty - 2010-04-21

    The issue actually also stems from the BaseEntity.ftl template which you can see uses a basic if statement to check if the table is the "SequenceValueItem" table to avoid placing the annotation to use the OpentapsIdentifierGenerator.

    Maybe one possible solution is to have a flag that can be set on the object before it is passed to BaseEntity.ftl so that we can designate certain entities to not use that annotation?

    As per the comment regarding hibernate generator will not try to regenerate a pk for the entity with a non-null value, this bug clearly seems to behave otherwise.

     
  • Si Chen

    Si Chen - 2010-04-30
    • status: open --> pending-works-for-me
     
  • SourceForge Robot

    • status: pending-works-for-me --> closed-works-for-me
     
  • SourceForge Robot

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     

Log in to post a comment.

MongoDB Logo MongoDB