my dog learned polymorphism*
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Who can solve that?" Watch "Who can solve that?" New topic
Author

Who can solve that?

Juan Fernandes
Greenhorn

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 [http://www.visualbuilder.com/java/hibernate/tutorial/pageorder/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" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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" />
</session-factory>
</hibernate-configuration>


///////////////////////////////////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)
ALTER TABLE [dbo].[PHONE_NUMBERS] DROP CONSTRAINT FK_PHONE_NUMBERS_USERS
GO

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]
GO

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

CREATE TABLE [dbo].[PHONE_NUMBERS] (
[USER_ID] [numeric](22, 0) NOT NULL ,
[NUMBER_TYPE] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[PHONE] [numeric](22, 0) NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[USERS] (
[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
) ON [PRIMARY]
GO




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


<?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>

<class name="com.visualbuilder.hibernate.User" table="USERS" >
<id name="userId" type="java.lang.Long" column="user_id" >
<generator class="increment" />
</id>
<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" />
</set>
</class>

</hibernate-mapping>


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


<?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>

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

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

</class>
</hibernate-mapping>


//////////////////////////////////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)numbers.next();
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
Name=CARLOS
Email=teste@xz.com
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(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.loadCollection(Loader.java:2001)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
at com.visualbuilder.hibernate.client.TestClient.testFindByPk(TestClient.java:91)
at com.visualbuilder.hibernate.client.TestClient.main(TestClient.java:176)
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(LongType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.persister.collection.AbstractCollectionPersister.readKey(AbstractCollectionPersister.java:734)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:975)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:646)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:591)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
... 9 more
Elanges waran
Greenhorn

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" />
</many-to-one>


Regards,<br />K.Elanges
 
 
subject: Who can solve that?
 
Similar Threads
could not initialize a collection:
Hibernate Association
Could not insert
hibernate migration
Hibernate Association