facebook

[Closed] Hibernate UserType

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #269181 Reply

    logris
    Member

    In my last project, I had need to trim strings coming from columns of type CHAR in an Informix database because all CHAR column values are retrieved as strings padded with spaces to fill the length of the column. The most elegant way I could find to solve the problem was to write my own hibernate UserType called TrimmedStringType that would trim the excess spaces off any string coming from the database if a property is configured to use it. The solution works quite well. This is a rough overview of the components of my setup:

    Snippet from my .hbm file for one of my CHAR type properties:

    <class name="...Office" table="Office">
      <property name="code" type="...TrimmedStringType">
    ...
    

    Sample of the model class used by Hibernate:

    class Office {
      String code;
      ...
    }

    Sample of TrimmedStringType:

    public class TrimmedStringType implements org.hibernate.usertype.UserType {
      public TrimmedStringType() {
        super();
      }
    
      public int[] sqlTypes() {
        return new int[] { Types.CHAR };
      }
    
      public Class returnedClass() {
        return String.class;
      }
      ...
    }
    

    In my new project, I need to do the same thing, but I am using the Hibernate Reverse Engineering wizard to generate my POJOs. I just tried a number of things using the RE tool to set a column’s “hibernate type” to TrimmedStringType, but nothing seems to produce the above configuration. The RE tool is building my POJO with properties of type TrimmedStringType, which is not the desired result.

    Is there any way to do this? If not, is it planned? If it isn’t yet planned, I would really like to be able to do this and would like to see it submitted as a feature request. Otherwise, I don’t see how it is possible to make use of a UserType, using the RE tool.

    #269182 Reply

    logris
    Member

    I am using Eclipse 3.2.2, and I the manual install of

    MyEclipse_5.5M2_E3.2.2_ManualInstall.zip

    #269216 Reply

    Brian Fernandes
    Moderator

    Hi,

    The usual technique would be to go to page 3 of the RE wizard, expand the table and select the column you are reverse engineering and type in your class name in the Hibernate type field.

    You said you tried a number of things, but I would need to know what you tried to help further.

    Could you tell me what version of ME you are using? Please keep an eye on the error log for any messages logged during the RE process. Sometimes there is an issue due to which all the RE settings are skipped during reverse engineering; in older ME versions this was only reported in the log. If you see a message stating “Error processing re settings file”, try deleting your existing hibernate.reveng.xml file and starting over with the settings mentioned above.

    Hope this helps.

    #269255 Reply

    logris
    Member

    Brian,

    First, I am using MyEclipse 5.5m2, with Eclipse 3.2.2. There are no errors in the logfile or Error Log window due to RE.

    When I navigate to page 3 of RE and set the Hibernate type to TrimmedStringType, no errors or exceptions are thrown. It looks as if everything works as intended. The mapping file is generated with the type=”TrimmedStringType” field as I stated above. This is desired behavior.

    However, my POJO is also generated with a variable that is an instance of class TrimmedStringType. This is not what I want. I need the POJO variable to be a java.lang.String, because TrimmedStringType returns String values. This is where I am at a loss.

    I have tried setting the JDBC type to CHAR, which (by default) should be mapped to java.lang.String. This is obviously not happening, although it may be intended behaviour nonetheless. I’m not really sure.

    I have also tried fiddling with the custom mappings on page 2 of RE, but similar results happen. If I set JDBC type CHAR to Hibernate Type TrimmedStringType, all of my CHAR columns get translated to POJO instance variables of class TrimmedStringType. Any further combination of JDBC type or Hibernate type set at the column level is still “all or nothing”. I get both the POJO type and the Hibernate type of exactly the same class.

    To me, it seems this feature was not implemented. I would like a confirmation that this is true, or a little help figuring out how to set my POJO to one class and my Hibernate property type to another.

    Thanks

    #269265 Reply

    logris
    Member

    This is starting to get frustrating… but at least I know it’s a “known issue”. After some research into some kind of workaround, I became aware of the real problem I am experiencing. It is detailed here:

    http://www.myeclipseide.com/PNphpBB2+file-viewtopic-t-14554.html

    I can’t help but note that this is the next release after 5.1. Is the issue getting any attention?

    #269332 Reply

    Brian Fernandes
    Moderator

    logris,

    I’m sorry I missed the point of your initial post. I’ve informed the team about this particular issue and we will try to get a fix in for 5.5GA.

    Thanks for following up and narrowing it down to this particular standing issue. I will update this thread with further info as it becomes available.

    #269457 Reply

    Brian Fernandes
    Moderator

    Logris,

    This problem has been fixed and will be released in 5.5GA.

    In the meanwhile, you can use this workaround:

    1) Follow steps in section 5.6 of the Hibernate Quickstart (Help > Contents > MyEclipse Learning Center > Spring / Hibernate Development > Hibernate Development)
    to create a custom RE strategy. The class you create must only override the constructor, nothing more is required.

    2) On page 2 of the RE wizard, point to the file you just created as mentioned in 5.6 step 4.

    3) On page 3, specify the custom mapping using an FQN for the type as usual.

    Doing the above will trigger a special class loader, which can detect the user types in your project and should generate appropriate code.

    The above workaround will not be required in 5.5GA.

    Thank you for your patience.

    #269605 Reply

    logris
    Member

    That worked perfectly. =)

    Thanks for your help!

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: [Closed] Hibernate UserType

You must be logged in to post in the forum log in