-->
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.  [ 5 posts ] 
Author Message
 Post subject: how to map sql text type of the property in hbm file
PostPosted: Wed Sep 14, 2005 1:46 pm 
Newbie

Joined: Thu Sep 01, 2005 10:35 am
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3

Name and version of the database you are using:SQL server



hi all,
I have one table in SQL data base which has so many columns and one of these columns is comment that type is text. i try to map all columns of that table.

Now my question is how to map comment column which is text type in database. becoz i try to map that column as a string and when i try to call the getter of that property then it throw the exception..


org.hibernate.exception.GenericJDBCException: could not initialize lazy properties: [com.onebeacon.office.ormapping.domain.Pending#3618]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:82)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.initializeLazyProperty(BasicEntityPersister.java:719)
at org.hibernate.intercept.FieldInterceptor.intercept(FieldInterceptor.java:60)
at org.hibernate.intercept.FieldInterceptor.readObject(FieldInterceptor.java:113)
at com.onebeacon.office.ormapping.domain.Pending.$cglib_read_Comments(Pending.java)
at com.onebeacon.office.ormapping.domain.Pending.getComments(Pending.java:245)
at com.Test.main(Test.java:63)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Value can not be converted to requested type. at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseData.getInteger(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unknown Source)
at org.hibernate.type.IntegerType.get(IntegerType.java:26)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:77)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:68)
at org.hibernate.persister.entity.BasicEntityPersister.initializeLazyProperty(BasicEntityPersister.java:702)
... 5 more





what i put in the type attribute of property tag of hbm file for that column.


so please help me...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 3:22 pm 
Newbie

Joined: Tue Sep 13, 2005 4:07 pm
Posts: 5
please post the mapping entry, the java getter and setter, and the define from the table that references the column in question.


Top
 Profile  
 
 Post subject: how to map sql text type of the property in hbm file
PostPosted: Wed Sep 14, 2005 3:37 pm 
Newbie

Joined: Thu Sep 01, 2005 10:35 am
Posts: 8
this is mapping file--------------

<class name="Pending" table="Pending">
<id name="POFID" type="java.lang.Integer" unsaved-value="null">
<column name="POFID" sql-type="java.lang.Integer" not-null="true" />
</id>
<property name="UserID" column="UserID" type="java.lang.String" length="25" lazy="true" />
<property name="Comments" type="java.lang.String" lazy="true"/>
</class>




and this is java class-------

private java.lang.String Comments ;


/**
* This methods sets the Comments attribute.
* @param Comments java.lang.String .
*/
public void setComments(java.lang.String Comments) {
this.Comments = Comments;
}

/**
* This method returns the Comments attribute.
* @return java.lang.String .
*/
public java.lang.String getComments() {
return Comments;
}


Top
 Profile  
 
 Post subject: how to map sql text type of the property in hbm file
PostPosted: Wed Sep 14, 2005 3:41 pm 
Newbie

Joined: Thu Sep 01, 2005 10:35 am
Posts: 8
And define the column in table as


comment(text,null)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 3:44 pm 
Newbie

Joined: Wed Sep 14, 2005 12:17 pm
Posts: 13
I haven't used Microsoft SQL Server very often, but I have a vague recollection that the TEXT type doesn't translate to the java.sql.Types properly. I've previously written (non-Hibernate) code where I've had to use a line like this:

"text".equalsIgnoreCase(metaData.getColumnTypeName(columnNumber))

to determine that the column is a TEXT column.

Based on the stack trace you've provided, Hibernate seems to think that the TEXT column is an Integer.

First thing to check is that your mapping is set up correctly; use something like this:

<property
name="myTextProperty"
type="java.lang.String"
access="property"
column="MY_TEXT_PROPERTY" />

If you've already tried that, then it's probably the case that Hibernate just can't handle TEXT columns. You could add support using a custom UserType. Something like this:

public class TextUserType implements UserType {

public int[] sqlTypes() {
// figure out what the right type is..
}

public Class returnedClass() {
return String.class;
}

public Object nullSafeGet(ResultSet resultSet,
String[] names, Object owner)
throws HibernateException, SQLException {
return resultSet.getString(names[0]);
}

public void nullSafeSet(
PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
statement.setString(index, value);
}
}

and then your property becomes:

<property
name="myTextProperty"
type="com.mypackagename.hibernate.TextUserType"
access="property"
column="MY_TEXT_PROPERTY" />


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