1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.util.Arrays ; 9 10 import org.hibernate.AssertionFailure; 11 import org.hibernate.HibernateException; 12 import org.hibernate.MappingException; 13 import org.hibernate.engine.EntityKey; 14 import org.hibernate.engine.ForeignKeys; 15 import org.hibernate.engine.Mapping; 16 import org.hibernate.engine.SessionImplementor; 17 import org.hibernate.persister.entity.EntityPersister; 18 19 23 public class ManyToOneType extends EntityType { 24 25 private final boolean ignoreNotFound; 26 27 protected boolean isNullable() { 28 return ignoreNotFound; 29 } 30 31 public boolean isAlwaysDirtyChecked() { 32 return ignoreNotFound; 33 } 34 35 public int getColumnSpan(Mapping mapping) throws MappingException { 36 return getIdentifierOrUniqueKeyType(mapping).getColumnSpan(mapping); 37 } 38 39 public int[] sqlTypes(Mapping mapping) throws MappingException { 40 return getIdentifierOrUniqueKeyType(mapping).sqlTypes(mapping); 41 } 42 43 public ManyToOneType(String className) { 44 this(className, false); 45 } 46 47 public ManyToOneType(String className, boolean lazy) { 48 super(className, null, !lazy, true, false); 49 this.ignoreNotFound = false; 50 } 51 52 public ManyToOneType( 53 String className, 54 String uniqueKeyPropertyName, 55 boolean lazy, 56 boolean unwrapProxy, 57 boolean isEmbeddedInXML, 58 boolean ignoreNotFound 59 ) { 60 super(className, uniqueKeyPropertyName, !lazy, isEmbeddedInXML, unwrapProxy); 61 this.ignoreNotFound = ignoreNotFound; 62 } 63 64 public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) 65 throws HibernateException, SQLException { 66 getIdentifierOrUniqueKeyType( session.getFactory() ) 67 .nullSafeSet(st, getIdentifier(value, session), index, settable, session); 68 } 69 70 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) 71 throws HibernateException, SQLException { 72 getIdentifierOrUniqueKeyType( session.getFactory() ) 73 .nullSafeSet(st, getIdentifier(value, session), index, session); 74 } 75 76 public boolean isOneToOne() { 77 return false; 78 } 79 80 public ForeignKeyDirection getForeignKeyDirection() { 81 return ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT; 82 } 83 84 public Object hydrate(ResultSet rs, String [] names, SessionImplementor session, Object owner) 85 throws HibernateException, SQLException { 86 87 90 Serializable id = (Serializable ) getIdentifierOrUniqueKeyType( session.getFactory() ) 91 .nullSafeGet(rs, names, session, null); 93 if (id!=null) scheduleBatchLoad(id, session); 94 95 return id; 96 } 97 98 101 private void scheduleBatchLoad(Serializable id, SessionImplementor session) 102 throws MappingException { 103 104 if (uniqueKeyPropertyName==null) { 106 EntityPersister persister = session.getFactory() 107 .getEntityPersister( getAssociatedEntityName() ); 108 109 EntityKey entityKey = new EntityKey( id, persister, session.getEntityMode() ); 110 session.getPersistenceContext() 111 .getBatchFetchQueue() 112 .addBatchLoadableEntityKey( entityKey ); 113 } 114 } 115 116 public boolean useLHSPrimaryKey() { 117 return false; 118 } 119 120 public boolean isModified(Object old, Object current, SessionImplementor session) 121 throws HibernateException { 122 123 if (current==null) return old!=null; 124 if (old==null) return current!=null; 125 return getIdentifierOrUniqueKeyType( session.getFactory() ) 127 .isDirty( old, getIdentifier(current, session), session ); 128 } 129 130 public Serializable disassemble(Object value, SessionImplementor session, Object owner) 131 throws HibernateException { 132 133 if ( isNotEmbedded(session) ) { 134 return getIdentifierType(session).disassemble(value, session, owner); 135 } 136 137 if (value==null) { 138 return null; 139 } 140 else { 141 Object id = ForeignKeys.getEntityIdentifierIfNotUnsaved( 144 getAssociatedEntityName(), 145 value, 146 session 147 ); 148 if (id==null) { 149 throw new AssertionFailure( 150 "cannot cache a reference to an object with a null id: " + 151 getAssociatedEntityName() 152 ); 153 } 154 return getIdentifierType(session).disassemble(id, session, owner); 155 } 156 } 157 158 public Object assemble(Serializable oid, SessionImplementor session, Object owner) 159 throws HibernateException { 160 161 164 Serializable id = (Serializable ) getIdentifierType(session) 165 .assemble(oid, session, null); 167 if ( isNotEmbedded(session) ) return id; 168 169 if (id==null) { 170 return null; 171 } 172 else { 173 return resolveIdentifier(id, session); 174 } 175 } 176 177 public boolean[] toColumnNullness(Object value, Mapping mapping) { 178 boolean[] result = new boolean[ getColumnSpan(mapping) ]; 179 if (value!=null) Arrays.fill(result, true); 180 return result; 181 } 182 183 } 184 | Popular Tags |