Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Hibernate and String trim problem

Thread view: 
DM - 29 Mar 2007 12:12 GMT
Hi all,
I use hibernate tools to create classes to store my db tables.
I have a problem with strings. I need to trim values but I want to do
it without to touch the automatically generated code...
What can I do?

Thanks all,
DM
Robert Klemme - 29 Mar 2007 12:15 GMT
> I use hibernate tools to create classes to store my db tables.
> I have a problem with strings. I need to trim values but I want to do
> it without to touch the automatically generated code...
> What can I do?

This is something you should do in application land, i.e. in your Java
classes.  Hibernate just transfers data from the application to the DB.

Kind regards

    robert
DM - 29 Mar 2007 13:09 GMT
> > I use hibernate tools to create classes to store my db tables.
> > I have a problem with strings. I need to trim values but I want to do
[quoted text clipped - 7 lines]
>
>         robert

I try to explain.
I load a table on a HashMap (the key of the table = the key of the
map) then I want to get the value from the map given the key.
The problem is the key in the db is i.e. "LOG          " (the string
is filled with spaces...).
If I call map.get("LOG") I don't found nothing... I've to call
map.get("LOG           "). This disturbs me...
I've inserted a .trim() on the constructor of the generated class, but
I wish to obtain the same result without touch it...

I've to wrap the generated class or can I use a different method?

Thanks,
DM
Lew - 29 Mar 2007 13:25 GMT
>>> I use hibernate tools to create classes to store my db tables.
>>> I have a problem with strings. I need to trim values but I want to do
[quoted text clipped - 18 lines]
>
> I've to wrap the generated class or can I use a different method?

If you can change the data model implementation, consider using VARCHAR
instead of CHAR for those columns.

-- Lew
Robert Klemme - 29 Mar 2007 14:43 GMT
>>>> I use hibernate tools to create classes to store my db tables.
>>>> I have a problem with strings. I need to trim values but I want to do
[quoted text clipped - 21 lines]
> If you can change the data model implementation, consider using VARCHAR
> instead of CHAR for those columns.

Yeah, change the data in the DB.  Or, if access is read only, create a
DB view that does the trimming and use that instead of the table.

    robert
DM - 29 Mar 2007 15:00 GMT
> >>>> I use hibernate tools to create classes to store my db tables.
> >>>> I have a problem with strings. I need to trim values but I want to do
[quoted text clipped - 26 lines]
>
>         robert

I work on a big project on a db with more than 40 tables and
distributed on many clients... changing the db is not a solution.

The best solution right now is to wrap java String in TrimString and
using this class instead of Strings... but the "tablename.hbm.xml"
file is automatically generated mapping db "char" into java String.
Can I map automatically "char" into TrimString?

Thanks,
DM
Robert Klemme - 29 Mar 2007 15:48 GMT
>>>>>> I use hibernate tools to create classes to store my db tables.
>>>>>> I have a problem with strings. I need to trim values but I want to do
[quoted text clipped - 23 lines]
> I work on a big project on a db with more than 40 tables and
> distributed on many clients... changing the db is not a solution.

Adding a view doesn't really hurt.

> The best solution right now is to wrap java String in TrimString and
> using this class instead of Strings... but the "tablename.hbm.xml"
> file is automatically generated mapping db "char" into java String.
> Can I map automatically "char" into TrimString?

Maybe.  You would have to hack hibernate or dialects I presume.

Regards

    robert
DM - 29 Mar 2007 16:22 GMT
> >>>>>> I use hibernate tools to create classes to store my db tables.
> >>>>>> I have a problem with strings. I need to trim values but I want to do
[quoted text clipped - 36 lines]
>
>         robert

I've found... on hibernate.org / Hibernate Users Faq / Tips and Tricks

How can I trim spaces from String data persisted to a CHAR column?
Use a UserType.

...doh... :-)

DM


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.