Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Hibernate Mapping to SmartGWT DataSource Mapping

    I my EJB container I have an hibernate annotated bean. Because the primary key spans over 4 attributes I use an extra Id class. I have to do it like this because otherwise my annotated named query in the entity will not work.

    My question:
    Is it either possible:
    - to use no xml based datasource mapping in SmartGWT?
    or:
    - to map the entity and its Id class properly in the xml based datasource mapping in SmartGWT?

    Hibernate cannot map the fields correctly if I annotate 4 Id fields within the same entity. So my entity has an Idclass attribute that I scope with id.x, id.y and so on in my annotated named query. Only in this case I get results via JPA (entity manager).

    It would be nice if there would either be a mapping solution for SmartGWT (for the listgrid) or just no xml mapping (I think of just regular programming the datasource mapping) ... Is that possible?

    Thank you.

    #2
    Both approaches work. The simpler one is the second: have a .ds.xml file, but add some DMI logic to map between your 4-way key and a simple, single String value that you send to the browser and receive from the browser.

    Comment


      #3
      Ok, if I understand this right, I configure my .ds.xml like this:
      - beanClassName is the class of my annotated entity
      - for all 4 keys the name of the field would be like id.x, id.y and so on?
      -> id is the name of the Id class attribute in my entity ...

      Do I have to specify/configure my Idclass anywhere?

      Comment


        #4
        No, you don't declare fields for the true primary keys at all. You just declare a single String field and use a DMI to modify the DSResponse so that your true keys are combined into a single String value, and for inbound dsRequests, split the single String into the values before you apply the properties to your bean.

        Comment


          #5
          The visual builder created one field in the .ds.xml specifiying it with name=id and type=%my Id class%. So that's incorrect, too?

          Is there an example for that DMI approach available?

          Comment


            #6
            You shouldn't generally be using Visual Builder to generate Hibernate DataSources, use autoDeriveSchema as covered in the QuickStart Guide.

            There's no automatic mapping to multiple primary keys, so that declaration in not correct.

            No, there's not an example of using Java to split and combine Strings in a DMI.

            Comment


              #7
              I have the following setup now:

              Datasource
              Code:
              <DataSource ID="aghsgrhsDataSource"
                serverConstructor="com.siemens.ibps.ui.server.AghsGrHsDataSource"
                schemaBean="com.siemens.ibps.ejb.domain.AghsGrHs" autoDeriveSchema="true">
                <serverObject className="com.siemens.ibps.ui.server.AghsGrHsDMIHandler" />
                <fields>
                  <field name="pk" type="text" hidden="true" />
                </fields>
              </DataSource>
              The DMI handler class has this method:
              Code:
              public DSResponse fetch(DSRequest dsRequest, HttpServletRequest request)
              The DataSource class looks like this:
              Code:
              public class AghsGrHsDataSource extends BasicDataSource {
              @Override
                public DSResponse executeFetch(DSRequest dsRequest) throws Exception
              }
              Within the executeFetch method I make a JNDI lookup for an EJB service facade that loads my list of entities via hibernate from the database. The entity has an Id class that contains 4 primary key attributes. After loading the data I call dsResponse.setData(data);.

              So far this all works but now to the dummy "pk" field in the .ds.xml datasource descriptor.

              - In which method should I modify the DSResponse (DMIHandler class or DataSource class)?
              - And how do I modify it? Do I use dsRequest.getDataSource().getField("pk")?
              - Does the pk field only contain the primary key attributes of the Idclass or the values, too?

              Right now I have all my columns of the entity visible in my ListGrid and in addition the "id" (Idclass attribute) column that is empty. I want to have one column for every attribute out of the Idclass in my ListGrid containing the values out of my loaded entity list.

              How do I manage to put the primary key attributes & their data (dsResponse.setData) into the pk field in order to visualize 4 additional columns in my ListGrid. It's just about managing the data now, I think it's clear of how to add the ListGrid columns programmatically ...

              Or do I have to load my entities in the DMIHandler class already?

              Thank you very much.
              Last edited by schmiuwe; 1 Jul 2011, 04:07.

              Comment


                #8
                You can put the code in either place. Use DataSource.getProperties() to turn your beans into Maps. Then you can freely add and remove properties, so you can easily turn your 4 PKs values into a single combined key.

                Comment


                  #9
                  Ok, i got it running but I had a different idea.

                  Besides my idclass in my entity I declared 4 additional transient attributes (one for every key attribute).

                  In executeFetch (after loading the data) I copy the values from the idclass to the transient attributes, they show up in the listgrid after this as being part of the datasource as fields.
                  -> Actually this can also be done by the ejb service method itself, then the ui does not need to implement it ...

                  So this seems just to be a pseudo revert to a flat entity structure with all attributes in one class, but nevertheless Hibernate does not execute the named query if I do not specify an idclass in case of a key that includes more than one attribute.

                  Comment

                  Working...
                  X