-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate and CHAR fields in primary keys
PostPosted: Fri Nov 03, 2006 2:06 pm 
Newbie

Joined: Fri Nov 03, 2006 1:44 pm
Posts: 4
I have an issue dealing with Hibernate and Oracle tables that have CHAR fields that participate in the primary key of these tables.

Specifically, we are tired of having to pad our objects' (char) instance variable values before we let hibernate retrieve and or update a row that has a CHAR in the primary key.

Yes, I agree, CHAR fields have no business being part of a primary key in any relational table, but, these table designs are from years and years ago, and, I guess, there's no desire to truely fix this by changing the offending database columns to be varchar....So, we're stuck for the time being dealing with this.

Further details: What happens is that, unless you pad the char variable out to the size of the CHAR field in the DB, you will not be able to retrieve/update an existing DB row b/c of how CHAR field mapping evidently works within Hibernate.

I have tried implementing our own TrimmedString UserType to no avail. This user-defined UserType seems to have no effect on hibernate's dealing's with primary key values, but does work for retrieving non-key CHAR values.

So, my question is, does anyone have any ideas on how to "fix" this so we don't have to pad our instance (char) variable values before letting hibernate do it's thing?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 2:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i guess your problem only has to do with querying right ?

if yes, do you remember to use that usertype in your queries ?

e..g .setParameter("id', Hibernate.custom(YourType.class)) ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 12:02 pm 
Newbie

Joined: Fri Nov 03, 2006 1:44 pm
Posts: 4
Thanks Max, I did *not* know to do that...I'll try it right away.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 5:01 pm 
Newbie

Joined: Fri Nov 03, 2006 1:44 pm
Posts: 4
Ok, I tried what Max suggested, and quite possibly due to my own ignorance of Hibernate, I am now getting the following exception when I try to select a row from my test table:

net.sf.hibernate.MappingException: Could not instantiate Type net.sf.hibernate.type.CustomType: java.lang.InstantiationException: net.sf.hibernate.type.CustomType

This is the stmt I'm trying to run:
str = (TrimmedStringTest) pSession.createQuery(mySQL)
.setParameter("qFirst", Hibernate.custom (com.chw.hibernate.usertypes.TrimmedString.class))
.uniqueResult();

where String mySQL = "FROM test_string ts WHERE ts.first = :qFirst ";

TrimmedStringTest is a simple POJO, and TrimmedString is defined as such:

package com.chw.hibernate.usertypes;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class TrimmedString implements UserType {
public TrimmedString() {
super();
}

public int[] sqlTypes() {
return new int[] { Types.CHAR };
}

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

public boolean equals(Object x, Object y) throws HibernateException {
return (x == y) || (x != null && y != null && (x.equals(y)));
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
String val = rs.getString(names[0]);
return val != null ? val.trim() : null;
}

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

public Object deepCopy(Object value) throws HibernateException {
if (null == value) return null;
return new String((String)value);
}

public boolean isMutable() {
return false;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 5:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is the full stacktrace?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 8:04 pm 
Newbie

Joined: Fri Nov 03, 2006 1:44 pm
Posts: 4
Full stack trace follows...I didn't mention the first time that I'm running my "tests" out of JUnit....

net.sf.hibernate.MappingException: Could not instantiate Type net.sf.hibernate.type.CustomType: java.lang.InstantiationException: net.sf.hibernate.type.CustomType
at net.sf.hibernate.type.TypeFactory.heuristicType(TypeFactory.java:156)
at net.sf.hibernate.impl.AbstractQueryImpl.guessType(AbstractQueryImpl.java:410)
at net.sf.hibernate.impl.AbstractQueryImpl.guessType(AbstractQueryImpl.java:405)
at net.sf.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:398)
at com.chw.gcm.dao.TrimmedStringTester.fetchTrimmedStringExplicitly(TrimmedStringTester.java:77)
at com.chw.gcm.dao.TrimmedStringTester.testRetrieveAndUpdate(TrimmedStringTester.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 09, 2006 2:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so use a debugger to step in and see why you are getting an InstantiationException...something went wrong with your usertype.

_________________
Max
Don't forget to rate


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