-->
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.  [ 3 posts ] 
Author Message
 Post subject: UserType with class of mutiple fields
PostPosted: Wed Aug 18, 2010 10:13 pm 
Newbie

Joined: Wed Aug 18, 2010 10:02 pm
Posts: 2
This might not be quite the right place, as I am working in Grails/Groovy but hopefully the basic idea is still the same.

I have a class that looks like this (along with various constructors and a few other methods):

Code:
class SampleValue {
   Float value
   Boolean real
}

It is supposed to abstract away the fact that I have a whole heap of fields that need to be storing two values, but the user enters them in a single field. I then parse that string into the two values.

I am trying to create the class to map this to the database (if possible). What I have so far is the below. I have been reading http://docs.jboss.org/hibernate/stable/ ... rType.html and I copied bits from http://omaha-seattle.blogspot.com/2010/ ... -type.html but I really need an example that does something similar to what I want.

Code:
import org.hibernate.usertype.UserType
import java.sql.Types
import java.sql.PreparedStatement
import java.sql.ResultSet

class SampleValueUserType implements org.hibernate.usertype.UserType {
   private static final SQL_TYPES = [Types.FLOAT, Types.BOOLEAN] as int[]

   public Object assemble(Serializable cached, Object owner) {
      cached
   }

   public Object deepCopy(Object o) {
      o
   }

   public Serializable disassemble(Object value)  {
      value
   }

   public boolean equals(Object x, Object y)  {
      x.equals(y)
   }

   public int hashCode(Object o) {
      o.hashCode()
   }

   public boolean isMutable() {
      true
   }

   public Object nullSafeGet(ResultSet rs, String[] names, Object owner) {
      def value = rs.getFloat(names[0])
      def real = rs.getBoolean(names[1])
      if (value && real) {
         return new SampleValue(value, real)
      } else {
         return null
      }
   }

   public void nullSafeSet(PreparedStatement st, Object value, int index) {
      // what is index??
      //st.setBigDecimal(index, (java.math.BigDecimal)value)
   }

   public Object replace(Object original, Object target, Object owner) {
      original
   }

   public Class returnedClass() {
      SampleValue
   }

   public int[] sqlTypes() {
      SQL_TYPES
   }
}


Top
 Profile  
 
 Post subject: Re: UserType with class of mutiple fields
PostPosted: Sun Sep 12, 2010 10:15 pm 
Newbie

Joined: Wed Aug 18, 2010 10:02 pm
Posts: 2
Bump.

Is doing this going to be possible?


Top
 Profile  
 
 Post subject: Re: UserType with class of mutiple fields
PostPosted: Mon Sep 13, 2010 2:26 am 
Newbie

Joined: Tue Aug 24, 2010 7:04 am
Posts: 14
Are you using JPA or Hibernate? In both cases I would prefer to do the transformation in your getter and setter. Than you have no reason to bind you to Hibernate.
Code:
@Entity public MyEntity {
private float value1;
private boolean value 2;

public MyType get() {
  return new MyType(value1, value2);
}

public void setMyType(MyType v) {
  value1 = v.getV1();
  value2 = v.getV2();
}}

If you want to query the MyType directly from Hibernat than you are bound to Hibernate because it is framwork specific. The following page is providing an example for custom types.

Kind regards
Michael


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