1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 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 [] PROP_NAMES = new String [] { 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 [] getPropertyNames() { 29 return PROP_NAMES; 30 } 31 32 public Type[] getPropertyTypes() { 33 return TYPES; 34 } 35 36 public int hashCode(Object x) throws HibernateException { 37 Multiplicity o = (Multiplicity) x; 38 return o.count + o.glarch.hashCode(); 39 } 40 41 public Object getPropertyValue(Object component, int property) { 42 Multiplicity o = (Multiplicity) component; 43 return property==0 ? 44 (Object ) new Integer (o.count) : 45 (Object ) o.glarch; 46 } 47 48 public void setPropertyValue( 49 Object component, 50 int property, 51 Object value) { 52 53 Multiplicity o = (Multiplicity) component; 54 if (property==0) { 55 o.count = ( (Integer ) 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 returnedClass() { 67 return Multiplicity.class; 68 } 69 70 public boolean equals(Object x, Object 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 nullSafeGet(ResultSet rs, String [] names, SessionImplementor session, Object owner) 79 throws HibernateException, SQLException { 80 81 Integer c = (Integer ) 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 st, Object value, int index, SessionImplementor session) 90 throws HibernateException, SQLException { 91 92 Multiplicity o = (Multiplicity) value; 93 GlarchProxy g; 94 Integer c; 95 if (o==null) { 96 g=null; 97 c=new Integer (0); 98 } 99 else { 100 g = o.glarch; 101 c = new Integer (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 deepCopy(Object 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 assemble( 122 Serializable cached, 123 SessionImplementor session, 124 Object owner) throws HibernateException { 125 if (cached==null) return null; 126 Serializable [] o = (Serializable []) cached; 127 Multiplicity m = new Multiplicity(); 128 m.count = ( (Integer ) 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 disassemble(Object value, SessionImplementor session) 136 throws HibernateException { 137 if (value==null) return null; 138 Multiplicity m = (Multiplicity) value; 139 return new Serializable [] { 140 new Integer (m.count), 141 ForeignKeys.getEntityIdentifierIfNotUnsaved( Glarch.class.getName(), m.glarch, session ) 142 }; 143 } 144 145 public Object replace(Object original, Object target, SessionImplementor session, Object owner) 146 throws HibernateException { 147 return assemble( disassemble(original, session), session, owner); 148 } 149 150 } | Popular Tags |