4 package eu
.etaxonomy
.cdm
.hibernate
;
6 import java
.io
.Serializable
;
7 import java
.math
.BigDecimal
;
8 import java
.sql
.PreparedStatement
;
9 import java
.sql
.ResultSet
;
10 import java
.sql
.SQLException
;
11 import java
.sql
.Types
;
13 import org
.hibernate
.HibernateException
;
14 import org
.hibernate
.engine
.spi
.SessionImplementor
;
15 import org
.hibernate
.type
.StandardBasicTypes
;
16 import org
.hibernate
.usertype
.UserType
;
19 * This software is public domain and carries NO WARRANTY.
21 * Patches, bug reports and feature requests welcome:
23 * https://bitbucket.org/ratkins/bigdecimalusertype/
25 public class BigDecimalUserType
implements UserType
{
27 private static final int[] SQL_TYPES
= new int[] {Types
.DECIMAL
, Types
.INTEGER
};
30 public Object
assemble(Serializable arg0
, Object arg1
) throws HibernateException
{
31 return deepCopy(arg0
);
35 public Object
deepCopy(Object arg0
) throws HibernateException
{
40 public Serializable
disassemble(Object arg0
) throws HibernateException
{
41 return (Serializable
) arg0
;
45 public boolean equals(Object arg0
, Object arg1
) throws HibernateException
{
46 return arg0
.equals(arg1
);
50 public int hashCode(Object arg0
) throws HibernateException
{
51 return arg0
.hashCode();
55 public boolean isMutable() {
60 public Object
nullSafeGet(ResultSet rs
, String
[] names
, SessionImplementor session
, Object owner
) throws HibernateException
, SQLException
{
61 BigDecimal bigDecimal
= (BigDecimal
) StandardBasicTypes
.BIG_DECIMAL
.nullSafeGet(rs
, names
, session
, owner
);
63 // BigDecimal bigDecimal = rs.getBigDecimal(names[0]);
64 if (bigDecimal
== null) {
67 return bigDecimal
.setScale(rs
.getInt(names
[1]), BigDecimal
.ROUND_HALF_UP
);
72 public void nullSafeSet(PreparedStatement st
, Object value
, int index
, SessionImplementor session
) throws HibernateException
, SQLException
{
74 StandardBasicTypes
.BIG_DECIMAL
.nullSafeSet(st
, null, index
, session
);
75 StandardBasicTypes
.INTEGER
.nullSafeSet(st
, null, index
, session
);
77 // st.setNull(index, Types.DECIMAL);
78 // st.setNull(index + 1, Types.INTEGER);
80 BigDecimal bdec
= (BigDecimal
)value
;
81 // st.setBigDecimal(index, bdec);
82 // st.setInt(index + 1, bdec.scale());
83 StandardBasicTypes
.BIG_DECIMAL
.nullSafeSet(st
, bdec
, index
, session
);
84 StandardBasicTypes
.INTEGER
.nullSafeSet(st
, bdec
.scale(), index
+ 1, session
);
90 public Object
replace(Object arg0
, Object arg1
, Object arg2
) throws HibernateException
{
95 public Class
<?
> returnedClass() {
96 return BigDecimal
.class;
100 public int[] sqlTypes() {