KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > legacy > MultiplicityType


1 //$Id: MultiplicityType.java,v 1.3 2005/04/28 15:44:16 oneovthafew Exp $
2
package org.hibernate.test.legacy;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import org.hibernate.Hibernate;
10 import org.hibernate.HibernateException;
11 import org.hibernate.engine.ForeignKeys;
12 import org.hibernate.engine.SessionImplementor;
13 import org.hibernate.type.Type;
14 import org.hibernate.usertype.CompositeUserType;
15
16 public class MultiplicityType implements CompositeUserType {
17
18     private static final String JavaDoc[] PROP_NAMES = new String JavaDoc[] {
19         "count", "glarch"
20     };
21     private static final int[] SQL_TYPES = new int[] {
22         Hibernate.INTEGER.sqlType(), Hibernate.STRING.sqlType()
23     };
24     private static final Type[] TYPES = new Type[] {
25         Hibernate.INTEGER, Hibernate.entity(Glarch.class)
26     };
27
28     public String JavaDoc[] getPropertyNames() {
29         return PROP_NAMES;
30     }
31
32     public Type[] getPropertyTypes() {
33         return TYPES;
34     }
35
36     public int hashCode(Object JavaDoc x) throws HibernateException {
37         Multiplicity o = (Multiplicity) x;
38         return o.count + o.glarch.hashCode();
39     }
40
41     public Object JavaDoc getPropertyValue(Object JavaDoc component, int property) {
42         Multiplicity o = (Multiplicity) component;
43         return property==0 ?
44             (Object JavaDoc) new Integer JavaDoc(o.count) :
45             (Object JavaDoc) o.glarch;
46     }
47
48     public void setPropertyValue(
49         Object JavaDoc component,
50         int property,
51         Object JavaDoc value) {
52
53         Multiplicity o = (Multiplicity) component;
54         if (property==0) {
55             o.count = ( (Integer JavaDoc) value ).intValue();
56         }
57         else {
58             o.glarch = (Glarch) value;
59         }
60     }
61
62     public int[] sqlTypes() {
63         return SQL_TYPES;
64     }
65
66     public Class JavaDoc returnedClass() {
67         return Multiplicity.class;
68     }
69
70     public boolean equals(Object JavaDoc x, Object JavaDoc y) {
71         Multiplicity mx = (Multiplicity) x;
72         Multiplicity my = (Multiplicity) y;
73         if (mx==my) return true;
74         if (mx==null || my==null) return false;
75         return mx.count==my.count && mx.glarch==my.glarch;
76     }
77
78     public Object JavaDoc nullSafeGet(ResultSet JavaDoc rs, String JavaDoc[] names, SessionImplementor session, Object JavaDoc owner)
79         throws HibernateException, SQLException JavaDoc {
80
81         Integer JavaDoc c = (Integer JavaDoc) Hibernate.INTEGER.nullSafeGet( rs, names[0] );
82         GlarchProxy g = (GlarchProxy) Hibernate.entity(Glarch.class).nullSafeGet(rs, names[1], session, owner);
83         Multiplicity m = new Multiplicity();
84         m.count = c==null ? 0 : c.intValue();
85         m.glarch = g;
86         return m;
87     }
88
89     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session)
90         throws HibernateException, SQLException JavaDoc {
91
92         Multiplicity o = (Multiplicity) value;
93         GlarchProxy g;
94         Integer JavaDoc c;
95         if (o==null) {
96             g=null;
97             c=new Integer JavaDoc(0);
98         }
99         else {
100             g = o.glarch;
101             c = new Integer JavaDoc(o.count);
102         }
103         Hibernate.INTEGER.nullSafeSet(st, c, index, session);
104         Hibernate.entity(Glarch.class).nullSafeSet(st, g, index+1, session);
105
106     }
107
108     public Object JavaDoc deepCopy(Object JavaDoc value) {
109         if (value==null) return null;
110         Multiplicity v = (Multiplicity) value;
111         Multiplicity m = new Multiplicity();
112         m.count = v.count;
113         m.glarch = v.glarch;
114         return m;
115     }
116
117     public boolean isMutable() {
118         return true;
119     }
120
121     public Object JavaDoc assemble(
122         Serializable JavaDoc cached,
123         SessionImplementor session,
124         Object JavaDoc owner) throws HibernateException {
125         if (cached==null) return null;
126         Serializable JavaDoc[] o = (Serializable JavaDoc[]) cached;
127         Multiplicity m = new Multiplicity();
128         m.count = ( (Integer JavaDoc) o[0] ).intValue();
129         m.glarch = o[1]==null ?
130             null :
131             (GlarchProxy) session.internalLoad( Glarch.class.getName(), o[1], false, false );
132         return m;
133     }
134
135     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session)
136     throws HibernateException {
137         if (value==null) return null;
138         Multiplicity m = (Multiplicity) value;
139         return new Serializable JavaDoc[] {
140                 new Integer JavaDoc(m.count),
141                 ForeignKeys.getEntityIdentifierIfNotUnsaved( Glarch.class.getName(), m.glarch, session )
142         };
143     }
144
145     public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, SessionImplementor session, Object JavaDoc owner)
146     throws HibernateException {
147         return assemble( disassemble(original, session), session, owner);
148     }
149     
150 }
Popular Tags