aspose file tools
The moose likes Object Relational Mapping and the fly likes Who can solve that? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Who can solve that?" Watch "Who can solve that?" New topic

Who can solve that?

Juan Fernandes

Joined: Jan 16, 2008
Posts: 2
Hi people a need a help, if someone give me a solution I will be very gratfull.
Using a Hibernate Tutorial I tried steps getting success, but when started
the step #16 []
that means: "Finding by primary key" I was very disapointed because I spent long days and
still does not work.
I tried "Using Native SQL" and too "Using Criteria Queries" and same error message appear.
My connection works fine (I think) with SQLServer.
My configurations and other information are in the next lines.
If more informations are requested I can send with pleasure.

Please help me someone!

Juan Fernandes.

/////////////////////////////// My hibernate.cfg.xml - it does works fine //////////////////////////////////////

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">
<property name="connection.url">jdbc dbc sn_2</property >
<!--property name="connection.url">jdbc:jtds:sqlserver://localhost/CEP_BD</property-->
<property name="connection.driver_class">sun.jdbc.odbc.JdbcOdbcDriver</property>
<property name="connection.username">Adm</property>
<property name="connection.password">123456</property>
<!-- Set AutoCommit to true -->
<property name="connection.autocommit">true</property>
<!-- SQL Dialect to use. Dialects are database specific -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Mapping files -->
<mapping resource="com/visualbuilder/hibernate/User.hbm.xml" />
<mapping resource="com/visualbuilder/hibernate/PhoneNumber.hbm.xml" />

///////////////////////////////////the generated tables in MSSQLServer/////////////////////////

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_PHONE_NUMBERS_USERS]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PHONE_NUMBERS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[PHONE_NUMBERS]

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[USERS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[USERS]

[USER_ID] [numeric](22, 0) NOT NULL ,
[NUMBER_TYPE] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[PHONE] [numeric](22, 0) NOT NULL

[USER_ID] [numeric](22, 0) NOT NULL ,
[FIRST_NAME] [varchar] (20) COLLATE Latin1_General_CI_AS NOT NULL ,
[LAST_NAME] [varchar] (20) COLLATE Latin1_General_CI_AS NOT NULL ,
[AGE] [numeric](22, 0) NOT NULL ,
[EMAIL] [varchar] (40) COLLATE Latin1_General_CI_AS NOT NULL

//////////////////////////////// the respectives User.hbm.xml the first////////////////////////////

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"" >

<class name="com.visualbuilder.hibernate.User" table="USERS" >
<id name="userId" type="java.lang.Long" column="user_id" >
<generator class="increment" />
<property name="firstName" type="java.lang.String" column="first_name" length="20" />
<property name="lastName" type="java.lang.String" column="last_name" length="20" />
<property name="age" type="java.lang.Integer" column="age" length="-1" />
<property name="email" type="java.lang.String" column="email" length="40" />
<set name="phoneNumbers" cascade="all">
<key column="USER_ID"/>
<one-to-many class="com.visualbuilder.hibernate.PhoneNumber" />


//////////////////////////////// the second PhoneNumber.hbm.xml /////////////////////////

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"" >

<class name="com.visualbuilder.hibernate.PhoneNumber" table="PHONE_NUMBERS" >
<key-property column="USER_ID" name="userId" type="java.lang.Long" />
<key-property column="NUMBER_TYPE" name="numberType" type="java.lang.String"/>

<property name="phone" type="java.lang.Long">
<column name="PHONE" precision="22" scale="0" />


//////////////////////////////////the method that generated exception/////////////////////////

public void testFindByPk(UserManager manager)
User user = manager.getUser(1);//Find the user with id=1
if(user == null) {
System.out.println("No user found with ID=1");
}else {
System.out.println("User found with ID=" + user.getUserId() + "\n" +
"\tName=" + user.getLastName() + "\n" +
"\tEmail=" + user.getEmail() +

java.util.Iterator numbers = user.getPhoneNumbers().iterator();
while(numbers.hasNext()) {
PhoneNumber ph = (PhoneNumber);
System.out.println("\t\tNumber Type:" + ph.getPhone() + "\n" +
"\t\tPhone Number:" + ph.getPhone());



//////////////////////// the resulted in my console //////////////////////

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
User found with ID=1
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not initialize a collection: <---THAT IS THE PROBLEM*****[com.visualbuilder.hibernate.User.phoneNumbers#1]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(
at org.hibernate.exception.SQLStateConverter.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.loader.Loader.loadCollection(
at org.hibernate.loader.collection.CollectionLoader.initialize(
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(
at org.hibernate.impl.SessionImpl.initializeCollection(
at org.hibernate.collection.AbstractPersistentCollection.initialize(
at org.hibernate.collection.PersistentSet.iterator(
at com.visualbuilder.hibernate.client.TestClient.testFindByPk(
at com.visualbuilder.hibernate.client.TestClient.main(
Caused by: java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]�ndice de descritor inv�lido
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataDouble(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataDouble(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getLong(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getLong(Unknown Source)
at org.hibernate.type.LongType.get(
at org.hibernate.type.NullableType.nullSafeGet(
at org.hibernate.type.NullableType.nullSafeGet(
at org.hibernate.persister.collection.AbstractCollectionPersister.readKey(
at org.hibernate.loader.Loader.readCollectionElement(
at org.hibernate.loader.Loader.readCollectionElements(
at org.hibernate.loader.Loader.getRowFromResultSet(
at org.hibernate.loader.Loader.doQuery(
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(
at org.hibernate.loader.Loader.loadCollection(
... 9 more
Elanges waran

Joined: Dec 13, 2007
Posts: 17
Hi Juan Fernandes,

I think you missed on 1-M table hbm( PhoneNumber.hbm.xml ) with relates 1-1 Table.

Simply put on PhoneNumber.hbm.xml in before </class> tag.

<many-to-one name="User" class="com.visualbuilder.hibernate.User"
cascade="all" >
<column name="user_id" />

Regards,<br />K.Elanges
It is sorta covered in the JavaRanch Style Guide.
subject: Who can solve that?