HBM2Java uses default plattform encoding for source generation

First post here. Hi everyone!

I am using hbm2java via Maven integration in a project where we are doing database first development. I have used a custom ReverseEngineeringStrategy in order to import the comments from tables and fields into the generated classes.

The project language is spanish, so usually the comments in the database will contain characters with accents (á, é, í…) etc. When the code is generated, it will produce errors because the files have the wrong encoding and these characters are mangled.

I took a look at the hbm2x sources and turns out that the code that does the actual writing of the files looks as follows (in the class org.hibernate.tool.hbm2x.TemplateProducer) :

 	fileWriter = new FileWriter(destination);
        fileWriter.write(tempResult);

The ‘destination’ variable is a File reference, and tempResult is the templates output.

Well, turns out that with this code, java will write the file using the default plattform encoding, which in my case, being a Windows user, will end up being CP-1512.

So, for international character codes support I think this code should at least write UTF-8, or even better, let the user select the encoding.

I can easily fork the project and make a pull request, but first I’d like some feedback so I don’t work in vain.

  • Should I open a bug report instead or are you OK with a PR?
  • If the latter, what requirements with regards to testing would you need me to fulfill?
  • Would I have to implement optional encoding or is it fine if I just default to UTF-8?

Thanks in advance, cheers to all.

Hello @jordihs,

Thanks for your post!

Yes we surely welcome contributions. Please create an issue in our JIRA (https://hibernate.atlassian.net/projects/HBX) and open a pull request on a corresponding topic branch.

Only implement UTF-8 if this is the only thing you need. This can be augmented in a later step.

Cheers,
Koen

Hi Koen, sorry it took so long to find some free time. I have created the issue and the pull request following your instructions. Here are links to those in case you want to check them out. Feel free to let me know if I did something wrong, as I don’t usually contribute (unfortunately), so I may have messed up.
The issue:
https://hibernate.atlassian.net/browse/BVAL-748
The PR:

Thank you, cheers.

Seems I messed up when I created the issue, I have asked for it to be deleted and created a new one at the right place. Here is the updated link:
https://hibernate.atlassian.net/browse/HBX-2014

Cheers.

I got greedy and created a new issue and PR. https://hibernate.atlassian.net/browse/HBX-2015

Please let me know if you like the spirit of this change, as I have quite a few in the same vein. I have a shema-first project going on where I have heavily customized the generation output but I had to do really ugly workarounds to get the OverrideRepository to play nice with my custom RevengStrategy implementation.

Thanks for your attention, cheers.