Project

General

Profile

Download (2.79 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.cdm.hibernate;
5

    
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;
12

    
13
import org.hibernate.HibernateException;
14
import org.hibernate.engine.spi.SessionImplementor;
15
import org.hibernate.type.StandardBasicTypes;
16
import org.hibernate.usertype.UserType;
17

    
18
/**
19
 * This software is public domain and carries NO WARRANTY.
20
 *
21
 * Patches, bug reports and feature requests welcome:
22
 *
23
 * https://bitbucket.org/ratkins/bigdecimalusertype/
24
 */
25
public class BigDecimalUserType implements UserType {
26

    
27
	private static final int[] SQL_TYPES = new int[] {Types.DECIMAL, Types.INTEGER};
28

    
29
	@Override
30
	public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
31
		return deepCopy(arg0);
32
	}
33

    
34
	@Override
35
	public Object deepCopy(Object arg0) throws HibernateException {
36
		return arg0;
37
	}
38

    
39
	@Override
40
	public Serializable disassemble(Object arg0) throws HibernateException {
41
		return (Serializable) arg0;
42
	}
43

    
44
	@Override
45
	public boolean equals(Object arg0, Object arg1) throws HibernateException {
46
		return arg0.equals(arg1);
47
	}
48

    
49
	@Override
50
	public int hashCode(Object arg0) throws HibernateException {
51
		return arg0.hashCode();
52
	}
53

    
54
	@Override
55
	public boolean isMutable() {
56
		return false;
57
	}
58

    
59
	@Override
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);
62

    
63
//		BigDecimal bigDecimal = rs.getBigDecimal(names[0]);
64
		if (bigDecimal == null) {
65
			return null;
66
		}
67
		return bigDecimal.setScale(rs.getInt(names[1]), BigDecimal.ROUND_HALF_UP);
68
	}
69
	
70

    
71
	@Override
72
	public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
73
		if (value == null) {
74
			StandardBasicTypes.BIG_DECIMAL.nullSafeSet(st, null, index, session);
75
			StandardBasicTypes.INTEGER.nullSafeSet(st, null, index, session);
76
			
77
//			st.setNull(index, Types.DECIMAL);
78
//			st.setNull(index + 1, Types.INTEGER);
79
		} else {
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);
85
		}
86
	}
87
	
88

    
89
	@Override
90
	public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
91
		return arg0;
92
	}
93

    
94
	@Override
95
	public Class<?> returnedClass() {
96
		return BigDecimal.class;
97
	}
98

    
99
	@Override
100
	public int[] sqlTypes() {
101
		return SQL_TYPES;
102
	}
103

    
104

    
105

    
106
}
(1-1/11)