-->
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.  [ 4 posts ] 
Author Message
 Post subject: Using Annotations w/ UserType (was infers column length)
PostPosted: Tue Jul 10, 2007 6:05 pm 
Newbie

Joined: Tue Jul 10, 2007 4:47 pm
Posts: 3
Found solution here: http://www.hibernate.org/Documentation/ ... 2AndOracle

My question is, how do I correctly use Annotations with my userType?

I have:

@org.hibernate.annotations.Type(
type = "persistent.types.FooType"
)
@Column(name = "Foo", nullable = false, length = 10)
private FooType foo;

The logs show that my trim is not happening.

and I get:

[emmajava] Caused by: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of persistent.entities.Baz.foo
[emmajava] at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:82)
[emmajava] at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
[emmajava] at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
[emmajava] at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3564)
[emmajava] at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
[emmajava] at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
[emmajava] at org.hibernate.loader.Loader.doQuery(Loader.java:729)
[emmajava] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
[emmajava] at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
[emmajava] at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
[emmajava] at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
[emmajava] at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3042)
[emmajava] at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
[emmajava] at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
[emmajava] at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
[emmajava] at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:195)
[emmajava] at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
[emmajava] at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
[emmajava] at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
[emmajava] at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
[emmajava] at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:175)
[emmajava] ... 26 more
[emmajava] Caused by: java.lang.IllegalArgumentException
[emmajava] at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
[emmajava] at java.lang.reflect.Field.set(Field.java:656)
[emmajava] at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:79)
[emmajava] ... 46 more

do I need to user a hibernate mapping xml?

----

was =>


Hibernate annotations 3.3.0.GA, Hibernate Core 3.2.3, Oracle 10g

For this Mapping:
@Column(name = "FOO", nullable = false, length = 10)
private String foo;

I get:
(extra 20 whitespace characters)
[DEBUG] (main) org.hibernate.type.StringType: returning '1234567890 ' as column: FOO1_
Tue Jul 10 21:53:44 2007 GMT MyTest 11931-0@desktop:0 [ERROR] (main) org.hibernate.util.JDBCExceptionReporter: ORA-12899: value too large for column "schema"."bar"."foo" (actual: 30, maximum: 10)

The oracle DDL is:
"FOO" CHAR(10 CHAR) NOT NULL ENABLE,

How do I tell Hibernate the column really is only a char(10)?

Thanks
-Steve


Top
 Profile  
 
 Post subject: Using OracleTypes.FIXED_TYPE
PostPosted: Wed Jul 11, 2007 3:15 pm 
Newbie

Joined: Tue Jul 10, 2007 4:47 pm
Posts: 3
I got the solution. Some notes:

Annotate your columns like this:


@org.hibernate.annotations.Type(
type = "OracleCHAR" // whatever your UserType is
)
@Column(name = "FOO" nullable = false, length = 19)
private String Foo;

And most important, extend the OracleDialect:

public class MyOracle9Dialect extends OracleDialect {

public MyoOracle9Dialect() {
super();
registerColumnType( OracleTypes.FIXED_CHAR, "char($l)");
}

}


Top
 Profile  
 
 Post subject: Create your type
PostPosted: Wed Jul 11, 2007 4:49 pm 
Newbie

Joined: Fri Apr 07, 2006 11:29 am
Posts: 17
This is pulled from: http://www.hibernate.org/hib_docs/annot ... ml#d0e2206

You must first create a named type, providing the typeClass as the UserType.

@TypeDefs(
{
@TypeDef(
name="caster",
typeClass = CasterStringType.class,
parameters = {
@Parameter(name="cast", value="lower")
}
)
}
)
package org.hibernate.test.annotations.entity;

...

Now use the type that you defined for your property.
public class Forest {
@Type(type="caster")
public String getSmallText() {
...
}


Top
 Profile  
 
 Post subject: "must" or "should"?
PostPosted: Wed Jul 11, 2007 5:58 pm 
Newbie

Joined: Tue Jul 10, 2007 4:47 pm
Posts: 3
Define "must"? Must as in, it is more correct? Or must as in to be JPA compliant and not use a Hibernate annotation?

The solution I posted "works correctly" in the sense that I am able to consume my entity and get my work done.


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