I've created my database in mysql, downloaded the JBOSS-IDE, reverse engineered my database design … everything looks good.
I have some 30+ tables. Most of the generated POJOs work, a few do not. What is common about those that do not work is that the table / POJO have a foreign key that is not of integer type – it is a varchar type.
Example, I have a table called States. In this table there is a column called time_zone_name. This column is a foreign key to the TimeZones table.
The error I get is: ‘Caused by: java.sql.SQLException: Invalid value for getInt() - 'MOUNTAIN' in column 2’
I updated hibernate tools JAR and the hibernate JARs but no luck. I’ve surfed the web and the only error close to mine is this post: ‘
http://forum.hibernate.org/viewtopic.php?t=962843&highlight=invalid+value+getint’.
Any help is appreciated.
This is the State.hbm.xml:
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">
<!-- Generated Sep 24, 2006 1:40:59 AM by Hibernate Tools 3.2.0.beta7 -->
<hibernate-mapping>
<class name="com.xyz.obj.State" table="states" catalog="_xyz_">
<comment></comment>
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<many-to-one name="timeZones" class="com.xyz.obj.TimeZone" fetch="select">
<column name="time_zone_name" length="8" not-null="true">
<comment></comment>
</column>
</many-to-one>
<many-to-one name="countries" class="com.xyz.obj.Country" fetch="select">
<column name="country_abbrv_name" length="3" not-null="true">
<comment></comment>
</column>
</many-to-one>
<property name="name" type="string">
<column name="name" length="20" not-null="true">
<comment></comment>
</column>
</property>
<property name="abbrvName" type="string">
<column name="abbrv_name" length="2" not-null="true" unique="true">
<comment></comment>
</column>
</property>
<set name="citieses" inverse="true">
<key>
<column name="state_abbrv_name" length="2" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="com.xyz.obj.City" />
</set>
</class>
</hibernate-mapping>
This is the TimeZone.hbm.xml:
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">
<!-- Generated Sep 24, 2006 1:40:59 AM by Hibernate Tools 3.2.0.beta7 -->
<hibernate-mapping>
<class name="com.xyz.obj.TimeZone" table="time_zones" catalog="_xyz_">
<comment></comment>
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="8" not-null="true" unique="true">
<comment></comment>
</column>
</property>
<set name="stateses" inverse="true">
<key>
<column name="time_zone_name" length="8" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="com.xyz.obj.State" />
</set>
</class>
</hibernate-mapping>
The is the SQL to create the states table:
Code:
create table states
(
id int not null auto_increment,
name varchar(20) not null,
abbrv_name char(2) not null,
country_abbrv_name varchar(3) not null,
time_zone_name varchar(8) not null,
constraint states_pk primary key(id),
constraint states_uk_abbrv_name unique(abbrv_name),
constraint states_fk_country_abbrv_name foreign key (country_abbrv_name) references countries(abbrv_name),
constraint states_fk_time_zone_name foreign key (time_zone_name) references time_zones(name)
) type=innodb;
Lastly, this is the SQL to create the time zone table:
Code:
create table time_zones
(
id int not null auto_increment,
name varchar(8) not null,
constraint time_zones_pk primary key(id),
constraint time_zones_uk_name unique(name)
) type=innodb;
This is my java call:
Code:
Criteria query = hibernateSession.createCriteria(State.class);
ArrayList states = (ArrayList) query.list();
And, and this is the stacktrace (set show_sql in conf file to true):
Code:
Hibernate: select this_.id as id23_0_, this_.time_zone_name as time2_23_0_, this_.country_abbrv_name as country3_23_0_, this_.name as name23_0_, this_.abbrv_name as abbrv5_23_0_ from _chuckspicks_.states this_
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
at org.hibernate.loader.Loader.list(Loader.java:2023)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:95)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at com.chuckspicks.CoversHistoryHTML.main(CoversHistoryHTML.java:27)
Caused by: java.sql.SQLException: Invalid value for getInt() - 'MOUNTAIN' in column 2
at com.mysql.jdbc.ResultSet.getIntFromString(ResultSet.java:2573)
at com.mysql.jdbc.ResultSet.getNativeInt(ResultSet.java:7722)
at com.mysql.jdbc.ResultSet.getNativeInt(ResultSet.java:7601)
at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2492)
at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2507)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:103)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2022)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1371)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2144)
... 6 more