Announcement Announcement Module
Collapse
No announcement yet.
Uppercase my data using Hibernate and Spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Uppercase my data using Hibernate and Spring?

    Is there an easy way to force all my String properties to be persisted UPPERCASE using hibernate and spring?

  • #2
    Sure, just modify your POJO setter to Upper the String given as a param for a property (you'll actually just use plain java not something particular to HB or Spring).

    Code:
    class MyPOJO
    {
    
    private String prop;
    
    public void setProp(String value)
    {
        prop = value.toUpperCase();
    }
    
    }

    Comment


    • #3
      Originally posted by costin
      Sure, just modify your POJO setter to Upper the String given as a param for a property (you'll actually just use plain java not something particular to HB or Spring).

      Code:
      class MyPOJO
      {
      
      private String prop;
      
      public void setProp(String value)
      {
          prop = value.toUpperCase();
      }
      
      }
      this code may throw a nullpointerexception :-)

      beside that, you can create a hibernate user-type, something like that:

      UpperCaseConverter extends Usertype {
      .... bla bla cut&paste sample code
      }

      define in your mapping file for your pojo:

      <hibernate-mapping>
      ............
      <property name="myPropertyName" type="UpperCaseConverter">


      this will ensure, that your property will be always converted to uppsercase, exactly when your object is saved into database. Or you can inlude a toLowerCase logic in ur usertype also.

      Comment


      • #4
        this code may throw a nullpointerexception
        True
        However, I would still recommend the simple java approach because:

        1. it's lightweight
        2. it doesn't depend on HB, it's just pure java
        3. you don't have to understand/work with usertypes
        4. your string comparison will be the same no matter if the data was persisted (upper cased) or not.

        Anyways, I recommend you use equalsIgnoreCase and the approapriate LIKE SQL command.

        Comment


        • #5
          and the approapriate LIKE SQL command.
          I am using hibernate, so I dont code my SQL.....what do you mean here?

          Thanks

          Comment


          • #6
            don't you write any queries (SQL or HQL) ? if you don't use LIKE then don't worry. If you are then make sure that your comparisons are case sensitive.

            Comment


            • #7
              Thanks, I'll give a try to the light option, i.e., not using UserType....

              Comment


              • #8
                re:usertypes

                Originally posted by jagarciaga
                Thanks, I'll give a try to the light option, i.e., not using UserType....
                look, it depends, whether this uppercase thing is only database issue, or is appearing also in the business logic.

                if you're converting into uppercase ONLY becasuse of database, you should abstract this database-issue by hibernate's UserType. It's also light, so don't speculate.

                If you are conveting into uppercase from other reasons, you may not use usertype.

                Comment


                • #9
                  Actually, it is a Database Issue, so should use Hibernate UserType.... Does using this approach imply changes on my POJOs or only in the mapping files?

                  Thanks.

                  Comment


                  • #10
                    Actually you can do the upper thing without usertype (any additional class) by using the function attribute inside your mapping files:

                    <property name="upperCaseName" formula="upper(name)" lazy="true"/>
                    For more info on this and user type check out the Hibernate reference documentation, the sample and even their tests.

                    Comment


                    • #11
                      Thanks Costin. This sounds much more like a clean solution. I am using however HIbernate 2, so as far as I understand this is not supported. I have to consider if it is worth migrating to 3 within my deadline requirements.
                      Thanks.

                      Comment


                      • #12
                        No, formulas are supported also in Hibernate 2. See: http://hibernate.sourceforge.net/hib...apping-2.0.dtd

                        Better check the support you have in the current version before doing any migration

                        Comment

                        Working...
                        X