merge trunk to hibernate4
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / UUIDUserType.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.hibernate;
11
12 import java.io.Serializable;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.sql.Types;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.hibernate.HibernateException;
21 import org.hibernate.engine.spi.SessionImplementor;
22 import org.hibernate.type.StandardBasicTypes;
23 import org.hibernate.usertype.UserType;
24 import org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType;
25 import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
26 import org.jadira.usertype.dateandtime.shared.spi.ColumnMapper;
27
28 /**
29 * @author a.mueller
30 * @created 22.07.2008
31 * @version 2.0
32 */
33 public class UUIDUserType extends AbstractUserType implements UserType {
34 static final long serialVersionUID = -3959049831344758708L;
35
36 @SuppressWarnings("unused")
37 private static final Logger logger = Logger.getLogger(UUIDUserType.class);
38
39 private static final int[] SQL_TYPES = { Types.VARCHAR };
40
41 /* (non-Javadoc)
42 * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
43 */
44 public Object deepCopy(Object o) throws HibernateException {
45 if (o == null) {
46 return null;
47 }
48
49 UUID uuid = (UUID) o;
50
51 try {
52 return UUID.fromString(uuid.toString());
53 } catch (IllegalArgumentException e) {
54 throw new HibernateException(e);
55 }
56 }
57
58 /* (non-Javadoc)
59 * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
60 */
61 public Serializable disassemble(Object value) throws HibernateException {
62 if(value == null) {
63 return null;
64 } else {
65 UUID uuid = (UUID) value;
66 return uuid.toString();
67 }
68 }
69
70 @Override
71 public UUID nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
72 throws HibernateException, SQLException {
73 String val = (String) StandardBasicTypes.STRING.nullSafeGet(rs, names, session, owner);
74
75 if(val == null) {
76 return null;
77 } else {
78
79 try {
80 return UUID.fromString(val);
81 } catch (IllegalArgumentException e) {
82 throw new HibernateException(e);
83 }
84 }
85 }
86
87 @Override
88 public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
89 throws HibernateException, SQLException {
90 if (value == null) {
91 // statement.setNull(index, Types.VARCHAR); old version
92 StandardBasicTypes.STRING.nullSafeSet(statement, value, index, session);
93 } else {
94 UUID uuid = (UUID)value;
95 // statement.setString(index, uuid.toString()); //old version
96 StandardBasicTypes.STRING.nullSafeSet(statement, uuid.toString(), index, session);
97 }
98 }
99
100
101 /* (non-Javadoc)
102 * @see org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType#returnedClass()
103 */
104 @Override
105 public Class returnedClass() {
106 return UUID.class;
107 }
108
109 @Override
110 public int[] sqlTypes() {
111 return SQL_TYPES;
112 }
113
114
115
116
117
118 }