Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4345538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:02:59+00:00 2026-05-21T12:02:59+00:00

I have a db table with column of datatype char(20). I’m not allowed to

  • 0

I have a db table with column of datatype char(20). I’m not allowed to change it to a varchar.

I’m writing a JPA entity mapped to this table. I would like the string field representing this column in my entity class to always contain the trimmed value, not the 20-character value padded with spaces that exists in the db.

I can’t see any easy way to do this. (an annotation would rock!). At the moment I’m just returning a trimmed value from my getter(), but this feels like a kludge.

A google search is offering no help on this. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:02:59+00:00Added an answer on May 21, 2026 at 12:02 pm

    Or you can use lifecycle annotations:

    @Entity
    public class MyEntity {
    
        @PostLoad
        protected void repair(){
            if(myStringProperty!=null)myStringProperty=myStringProperty.trim();
        }
    
        private String myStringProperty;
        public String getMyStringProperty() {
            return myStringProperty;
        }
        public void setMyStringProperty(String myStringProperty) {
            this.myStringProperty = myStringProperty;
        }
    
    }
    

    If this occurs on multiple entities you can create a custom annotation and write a dedicated EntityListener.

    Annotation

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Trim {}
    

    Listener

    public class TrimListener {
    
        private final Map<Class<?>, Set<Field>> trimProperties = 
            new HashMap<Class<?>, Set<Field>>();
    
        @PostLoad
        public void repairAfterLoad(final Object entity) throws Exception {
            for (final Field fieldToTrim : getTrimProperties(entity.getClass())) {
                final String propertyValue = (String) fieldToTrim.get(entity);
                if (propertyValue != null)
                    fieldToTrim.set(entity, propertyValue.trim());
            }
        }
    
        private Set<Field> getTrimProperties(Class<?> entityClass) throws Exception {
            if (Object.class.equals(entityClass))
                return Collections.emptySet();
            Set<Field> propertiesToTrim = trimProperties.get(entityClass);
            if (propertiesToTrim == null) {
                propertiesToTrim = new HashSet<Field>();
                for (final Field field : entityClass.getDeclaredFields()) {
                    if (field.getType().equals(String.class)
                        && field.getAnnotation(Trim.class) != null) {
                        field.setAccessible(true);
                        propertiesToTrim.add(field);
                    }
                }
                trimProperties.put(entityClass, propertiesToTrim);
            }
            return propertiesToTrim;
        }
    
    }
    

    Now annotate all relevant String fields with @Trim and register the Listener as default entity listener in your persistence.xml:

    <persistence-unit ..>
        <!-- ... -->
        <default-entity-listeners>
          com.somepackage.TrimListener
          and.maybe.SomeOtherListener
        </default-entity-listeners>
    </persistence-unit>
    

     

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with nvarchar(max) datatype column. Max length of data in this
I have a table with the column name: variant_markup datatype: numeric(4,3) I try to
I have some data in an SQL table with an XML datatype column where
I have a DATE datatype mysql column in a table. I want to set
I have table with column Percentage varchar(10) Data in that table is Pecentage 2/10
I have a table with 800+ records. In this table I have a column
I have a table that contains a column of XML Datatype (column name FileContent).
In my SQL Server table I have a column of datatype timestamp and I
in my SQL Server 2005 database I have a column RMA_Number with datatype char(10)
Here i have table test having column id with datatype nvarchar contains values like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.