-->
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.  [ 11 posts ] 
Author Message
 Post subject: odd Hibernate Error: StringIndexOutOfBoundsException:?!
PostPosted: Fri Sep 17, 2004 12:31 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
2.1.6

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.fangtoo.bean.Sellhouse" table="sell_house">
<id name="sell_id" column="sell_id" type="string">
<generator class="com.fangtoo.util.HibernateGenerator"/>
</id>
<property name="province">
<column name="province" sql-type="varchar(20)"/>
</property>
<property name="city">
<column name="city" sql-type="varchar(20)"/>
</property>
<property name="district">
<column name="district" sql-type="varchar(20)"/>
</property>
<property name="location">
<column name="location" sql-type="varchar(200)"/>
</property>
<property name="room">
<column name="room" sql-type="smallint"/>
</property>
<property name="guestroom">
<column name="guestroom" sql-type="smallint"/>
</property>
<property name="washroom">
<column name="washroom" sql-type="smallint"/>
</property>
<property name="area">
<column name="area" sql-type="float"/>
</property>
<property name="netarea">
<column name="netarea" sql-type="float"/>
</property>
<property name="total_price">
<column name="total_price" sql-type="smallint"/>
</property>
<property name="unit_price">
<column name="unit_price" sql-type="int"/>
</property>
<property name="building">
<column name="building" sql-type="varchar(100)"/>
</property>
<property name="building_type">
<column name="building_type" sql-type="varchar(20)"/>
</property>
<property name="floor">
<column name="floor" sql-type="smallint"/>
</property>
<property name="kitchen">
<column name="kitchen" sql-type="smallint"/>
</property>
<property name="toilet">
<column name="toilet" sql-type="smallint"/>
</property>
<property name="total_floor">
<column name="total_floor" sql-type="smallint"/>
</property>
<property name="equipment">
<column name="equipment" sql-type="varchar(200)"/>
</property>
<property name="facilities">
<column name="facilities" sql-type="varchar(200)"/>
</property>
<property name="environment">
<column name="environment" sql-type="varchar(200)"/>
</property>
<property name="orientation">
<column name="orientation" sql-type="varchar(10)"/>
</property>
<property name="build_year">
<column name="build_year" sql-type="smallint"/>
</property>
<property name="ownership">
<column name="ownership" sql-type="varchar(10)"/>
</property>
<property name="fitment">
<column name="fitment" sql-type="varchar(10)"/>
</property>
<property name="maintenance_fee">
<column name="maintenance_fee" sql-type="varchar(50)"/>
</property>
<property name="transportation">
<column name="transportation" sql-type="varchar(200)"/>
</property>
<property name="detail">
<column name="detail" sql-type="longtext"/>
</property>

<property name="linkman">
<column name="linkman" sql-type="varchar(20)"/>
</property>

<property name="linkman_cell_phone">
<column name="linkman_cell_phone" sql-type="varchar(50)"/>
</property>
<property name="linkman_email">
<column name="linkman_email" sql-type="varchar(100)"/>
</property>
<property name="linkman_phone">
<column name="linkman_phone" sql-type="varchar(50)"/>
</property>
<property name="linkman_contact_phone">
<column name="linkman_contact_phone" sql-type="varchar(50)"/>
</property>
<property name="internal_id">
<column name="internal_id" sql-type="varchar(40)"/>
</property>
<property name="owner_memo">
<column name="owner_memo" sql-type="longtext"/>
</property>
<property name="owner_phone">
<column name="owner_phone" sql-type="varchar(50)"/>
</property>
<property name="owner_name">
<column name="owner_name" sql-type="varchar(20)"/>
</property>
<property name="owner_sex">
<column name="owner_sex" sql-type="char(2)"/>
</property>
<property name="house_license">
<column name="house_license" sql-type="longblob"/>
</property>
<property name="brief_name">
<column name="brief_name" sql-type="varchar(40)"/>
</property>
<property name="house_state">
<column name="house_state" sql-type="varchar(10)"/>
</property>
<property name="valid_date">
<column name="valid_date" sql-type="date"/>
</property>
<property name="create_date">
<column name="create_date" sql-type="date"/>
</property>
<property name="allow_broker">
<column name="allow_broker" sql-type="varchar(10)"/>
</property>
<property name="deal_price">
<column name="deal_price" sql-type="float"/>
</property>


<many-to-one name="broker" class="com.fangtoo.bean.Broker" column="broker_id"/>
<many-to-one name="userinfo" class="com.fangtoo.bean.Userinfo" column="user_id" not-null="true"/>
</class>
</hibernate-mapping>


List list=s.find("from Sellhouse");
System.out.println("Get LIst: "+list.toString());


INFO: building session factory
2004-9-17 12:21:54 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance

INFO: Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 0
at java.lang.String.charAt(String.java:444)
at net.sf.hibernate.type.CharacterType.get(CharacterType.java:23)
at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
at net.sf.hibernate.type.AbstractType.hydrate(AbstractType.java:66)
at net.sf.hibernate.loader.Loader.hydrate(Loader.java:686)
at net.sf.hibernate.loader.Loader.loadFromResultSet(Loader.java:627)
at net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:586)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:501)
at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:213)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:281)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections
(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1521)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1513)
at com.fangtoo.bean.TestHiber.main(TestHiber.java:126)
Press any key to continue...


3.23.56

No display yet

INFO:


Hi everyone:

I come across a odd problem.When I run this simple console code:
Code:
public class TestHiber {
     static SessionFactory sf=null;
   
   public static void main(String[] args)throws Exception {

Session s=HibernateUtil.currentSession();
      Transaction t=s.beginTransaction();
List list=s.find("from Sellhouse");
      System.out.println("Get LIst: "+list.toString());
      t.commit();
      HibernateUtil.closeSession(); 
              }
}


I get a error in console:
Code:
java.lang.StringIndexOutOfBoundsException


Why. My code is simple ,and it is not use string[] or list.get() .. etc. Why it out of range? Thank you!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 3:51 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Hello Yashnoo,

This is problably because of the .toString() implementation of the class implementing the List interface. In Hibernate this problably is an instance of net.sf.hibernate.collection.List. Check the sourcecode for this file on how it implements the .toString() method. This problably leeds you to your StringIndexOutOfBounds exception.
Hope this helps.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject: :(
PostPosted: Fri Sep 17, 2004 6:13 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
I think problem maybe not this. I change my code to:
Code:

public class TestHiber {
     static SessionFactory sf=null;
   
   public static void main(String[] args)throws Exception {

Session s=HibernateUtil.currentSession();
      Transaction t=s.beginTransaction();
java.util.List list=s.find("from Sellhouse");
      t.commit();
      HibernateUtil.closeSession(); 
              }
}


It can't work too. The Exception remaind happen. The following code will throw the same exception:
Code:
Sellhouse sellhouse=(Sellhouse)session.load(Sellhouse.class,sell_id);

Why? The sell_house table can't be operated using Hibernate. If I change the code to JDBC:
Code:
String sql="select * from sell_house";
Connection conn=session.connection();
Statement stm=conn.createStatement();
ResultSet rs=stm.executeQuery(sql);
while(rs.next()){
  String sell_name=rs.getString("sell_name");
  String sell_date=rs.getString("sell_date");
}


Now it works well! I mean that the database is not the problem. So I think the exception will happen If I use Hibernate here. HOw to solve the problem.Why couldn't I use Hibernate here? THks!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 18, 2004 5:36 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
From the stacktrace it seems the CharacterType got an empty string from the resultset (not null - but empty) - hence causing the exception when it tries to retrieve the first character of the string...

Now, a CharacterType is used to map an SQL CHAR(1) to a Java Character - and can't see any places where it should be used in your mapping...

... except for the 'owner_sex' attribute which is mapped using an SQL char(2) field.

This should give you some hint about the possible cause of your problem.
Try to increase Hibernate's log level to DEBUG and you will probably see which field is actually causing problem.


Top
 Profile  
 
 Post subject: :)
PostPosted: Sat Sep 18, 2004 10:53 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi bertrand:


After I change my database from 3.2.X to 4.0,No problem! It works well and don't change my any code.The Exception dependence database?

Why it can work property if I change my database? As I know,Hibernate don't dependent database version, right? :(

_________________
You are not alone...


Top
 Profile  
 
 Post subject: :(
PostPosted: Sun Sep 19, 2004 1:57 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
No No! The problem remain! When I invoke the following code:

Code:


      Transaction t=_session.beginTransaction();
      Leasehouse leasehouse=(Leasehouse)_session.load(Leasehouse.class,leasehouseID);
      _session.flush();
      t.commit();


The Tomcat throw the same Exception! :
Code:
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
   java.lang.String.charAt(String.java:460)
   net.sf.hibernate.type.CharacterType.get(CharacterType.java:23)
   net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
   net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
   net.sf.hibernate.type.AbstractType.hydrate(AbstractType.java:66)
   net.sf.hibernate.loader.Loader.hydrate(Loader.java:686)
   net.sf.hibernate.loader.Loader.loadFromResultSet(Loader.java:627)
   net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:586)
   net.sf.hibernate.loader.Loader.getRow(Loader.java:501)
   net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:213)
   net.sf.hibernate.loader.Loader.doQuery(Loader.java:281)
   net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
   net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
   net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
   net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
   net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
   net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
   net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
   net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
   net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1920)
   com.fangtoo.persistence.service.LeasehouseDAOImp.getLeasehouse(LeasehouseDAOImp.java:200)
   org.apache.jsp.my_002dchuzu_002ddetail_jsp._jspService(my_002dchuzu_002ddetail_jsp.java:462)
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
   com.fangtoo.house.servlet.EncodingFilter.doFilter(EncodingFilter.java:24)



Why? Can anyone give me a answer?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 3:36 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
As someone above said it is probably the owner sex field being empty in one of the rows in your table.

Try this in MySQL ( NOT through Hibernate )

Code:
select * from sell_house where length( owner_sex ) < 2


to find the row(s) that are causing problems. I think ( someone correct me if I am wrong ) that if you define a property as a char(x) type then the length of the data must always be EXACTLY x characters.


Top
 Profile  
 
 Post subject: :)
PostPosted: Sun Sep 19, 2004 4:20 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
You are right! I change all the char field to varchar(10) , it works well! Thank you!
mm...

But why hibernate don't check length of char and avoid this Exception happen? :-)


Thks all!

_________________
You are not alone...


Top
 Profile  
 
 Post subject: Re: :)
PostPosted: Sun Sep 19, 2004 4:18 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Yashnoo wrote:
But why hibernate don't check length of char and avoid this Exception happen? :-)


Because the Hibernate Character type is used to map an SQL CHAR(1) to a Java Character - not a char(2)

I noticed you have specified the SQL types explicitly for all your fields. I'm not sure it is the safest way to build your mapping (unless you are working with a legacy schema). You should rather let Hibernate discover the actual JAVA type of your properties (by reflection) and let him deduced the appropriate SQL type.

(to be honest, I've never tried to use the SQL char type with a lenght greater than 1... so I don't know how you should have done it :(


Top
 Profile  
 
 Post subject: Re: :)
PostPosted: Mon Sep 20, 2004 1:26 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
bertrand wrote:

I noticed you have specified the SQL types explicitly for all your fields. I'm not sure it is the safest way to build your mapping (unless you are working with a legacy schema). You should rather let Hibernate discover the actual JAVA type of your properties (by reflection) and let him deduced the appropriate SQL type.(


But if you have to setting the precision SQL Type ,how to do it? As I know ,if I need set the precision SQL Type,I need have to using the Attribute
Code:
sql-type="varchar(200)"
. For example, I want to set the name field to varchar(30), how to do it? I need using the [strong]sql-type[/strong] in the Mapping file. Because I want to use the mapping create my table constructor. Hibernate need I set this info to it so hibernate can create the property field. If I use the
Code:
type="string"
,hibernate will know nothing,for example the varchar length.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 4:19 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
what if you use:

Code:
type="string" sql-type="char(2)"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.