-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate string truncation
PostPosted: Thu Sep 15, 2005 2:46 am 
Newbie

Joined: Mon Sep 12, 2005 11:01 am
Posts: 1
I have mapping file for a class contains <column> element for each mapped property. In <column> element indicated length property according to database column length. But when property value more that specified length an exception throws during insert/update statetement execution. So is anybody know how can i enable hibernate to truncate string properties so they will fit into database columns according to length property of <column> element in mapping file. Or if it is some other technique to made such thing (auto string truncation) work in hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 12:47 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
What database are you using? Have you tried getting rid of the length attribute altogether and letting the DB do the truncation ?

If this doesn't work, look into writing an Interceptor. Perhaps someone else can tell us if "interception" occurs before or after the point at which H scrutnizes persistent Strings for length constraints.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 1:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
http://hibernate.org/118.html#A17


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 1:00 pm 
Newbie

Joined: Thu Sep 22, 2005 12:29 pm
Posts: 1
Here is my code
Code:
public class VarCharString implements UserType, ParameterizedType {

   private int length;
   public void setParameterValues(Properties parameters) {
        length = Integer.parseInt(parameters.getProperty("length"));
   }
   ...............
}


and here is my mappings
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

   <typedef name="FirstName"
      class="default.VarCharString">
      <param name="length">30</param>
      </typedef>

   <!-- rename class to student card -->
    <class name="default.Student" table="studentcard" schema="public" lazy="false">
        <property name="firstname" type="FirstName">
            <column name="firstname" length="30" />
        </property>
        <property name="middlename" type="string">
            <column name="middlename" length="40" />
        </property>
        <property name="lastname" type="string">
            <column name="lastname" length="50" />
        </property>
   </class>
</hibernate-mapping>


The question is: should I define
Code:
<typedef name="MiddleName"
      class="default.VarCharString">
      <param name="length">40</param>
      </typedef>

and
Code:
   <typedef name="LastName"
      class="default.VarCharString">
      <param name="length">50</param>
      </typedef>

or there is a way to force ParameterizedType.setParameterValues() to "read" length param from here
Code:
        <property name="firstname" type="FirstName">
            <column name="firstname" length="30" />
        </property>

maybe there are some other ways to slove this problem?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.