KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > ClassType


1 //$Id: ClassType.java,v 1.2 2004/09/25 11:22:19 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 import org.hibernate.Hibernate;
9 import org.hibernate.HibernateException;
10 import org.hibernate.util.ReflectHelper;
11
12 /**
13  * <tt>class</tt>: A type that maps an SQL VARCHAR to a Java Class.
14  * @author Gavin King
15  */

16 public class ClassType extends ImmutableType {
17
18     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
19         String JavaDoc str = (String JavaDoc) Hibernate.STRING.get(rs, name);
20         if (str == null) {
21             return null;
22         }
23         else {
24             try {
25                 return ReflectHelper.classForName(str);
26             }
27             catch (ClassNotFoundException JavaDoc cnfe) {
28                 throw new HibernateException("Class not found: " + str);
29             }
30         }
31     }
32
33     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
34         //TODO: would be nice to handle proxy classes elegantly!
35
Hibernate.STRING.set(st, ( (Class JavaDoc) value ).getName(), index);
36     }
37
38     public int sqlType() {
39         return Hibernate.STRING.sqlType();
40     }
41
42     public String JavaDoc toString(Object JavaDoc value) throws HibernateException {
43         return ( (Class JavaDoc) value ).getName();
44     }
45
46     public Class JavaDoc getReturnedClass() {
47         return Class JavaDoc.class;
48     }
49
50     public String JavaDoc getName() {
51         return "class";
52     }
53
54     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
55         try {
56             return ReflectHelper.classForName(xml);
57         }
58         catch (ClassNotFoundException JavaDoc cnfe) {
59             throw new HibernateException("could not parse xml", cnfe);
60         }
61     }
62
63 }
64
65
66
67
68
69
70
Popular Tags