-->
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: mapping postgresql "create type"
PostPosted: Thu Dec 06, 2007 4:56 am 
Newbie

Joined: Thu Dec 06, 2007 4:28 am
Posts: 2
I have a user defined type ..
Code:
CREATE TYPE mymoney AS (
        local_value numeric(11,2),
        mnemonic character(3)
);

used in a table ...
Code:
create table prices (id serial, price mymoney);


In Java I have an @Entity class for Prices and a class for MyMoney

Any idea how I map this using Hibernate?

Right now I'm getting ...
Code:
org.hibernate.util.JDBCExceptionReporter - could not insert: [Prices] [insert into Prices (i2, price) values (?, ?)]
org.postgresql.util.PSQLException: ERROR: column "price" is of type mymoney but expression is of type bytea


which I guess means that Hibernate is trying to serialize the object and call statement.setBytes() instead of calling statement.setObject();

best wishes
Roy


Top
 Profile  
 
 Post subject: SOLVED mapping postgresql "create type"
PostPosted: Thu Dec 06, 2007 3:16 pm 
Newbie

Joined: Thu Dec 06, 2007 4:28 am
Posts: 2
Gosh that was sooo easy.

For anybody else, here's the scoop.

I want to create a Custom Data Type in PostgreSQL capable of storing a combination of local currency amount, currency code, exchange rate and base currency amount.

So in psql ...

Code:
CREATE TYPE mymoney AS (
        local_value numeric(11,2),
        mnemonic character(3),
        exchange_rate numeric(11,2),
        base_value numeric(11,2)
);
CREATE TABLE expense (id serial, amount mymoney);


To map this to Hibernate, the most important thing to do is make sure that the class for MyMoney implements org.postgresql.util.PGobject, overriding methods as necessary.

The mapping is done with
Code:
public class MoneyUserType implements UserType {
   public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
         throws HibernateException, SQLException {
      assert names.length == 1;
      if (resultSet.wasNull()) {
         return null;
      }
      final Money money = new Money(resultSet.getObject(names[0]).toString());
      return money;
   }
   public void nullSafeSet(PreparedStatement statement, Object value, int index)
         throws HibernateException, SQLException {
      statement.setObject(index, value);
   }
}

//NB This is an abbreviation of my actual class


So now I can simply do ...

Code:
Expense expense = new Expense();
expense.setAmount(new Money("(100,PHP,.90,111.11)"));
session.save(expense);


hbm.xml contains
Code:
   <property name="amount" type="hibnoa.MoneyUserType">
         <column name="price" sql-type="MONEY_TY"/>
      </property>



I realise that by extending PGobject, I've made my code database dependent. Hopefully at some point the PostgreSQL JDBC driver will support SQLdata in which case there is a portable alternative available. In the meantime it would take all the wild horses in Argentina to make me change database so that's not really an issue.

best
Roy


Top
 Profile  
 
 Post subject: Re: SOLVED mapping postgresql "create type"
PostPosted: Wed Mar 14, 2012 12:16 pm 
Newbie

Joined: Wed Mar 14, 2012 11:31 am
Posts: 2
I have a problem similar to yours....but I am doing a reverse engineering by using hibernate tools....however I have not understand what do you put in
public int[] sqlTypes() ; and what do you do with new Money(String data)....do you parse the string rapresentation of the Money Object?.
Regards

P.S.
I know it is a very old post....but i try...
rsmithh wrote:
Gosh that was sooo easy.
.....
The mapping is done with
Code:
public class MoneyUserType implements UserType {
   public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
         throws HibernateException, SQLException {
      assert names.length == 1;
      if (resultSet.wasNull()) {
         return null;
      }
      final Money money = new Money(resultSet.getObject(names[0]).toString());
      return money;
   }
   public void nullSafeSet(PreparedStatement statement, Object value, int index)
         throws HibernateException, SQLException {
      statement.setObject(index, value);
   }
}

//NB This is an abbreviation of my actual class

.....

best
Roy


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.