KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: SpecialOneToOneType.java,v 1.10 2005/06/20 20:32:36 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 import org.hibernate.AssertionFailure;
9 import org.hibernate.HibernateException;
10 import org.hibernate.MappingException;
11 import org.hibernate.engine.ForeignKeys;
12 import org.hibernate.engine.Mapping;
13 import org.hibernate.engine.SessionImplementor;
14
15 /**
16  * A one-to-one association that maps to specific formula(s)
17  * instead of the primary key column of the owning entity.
18  *
19  * @author Gavin King
20  */

21 public class SpecialOneToOneType extends OneToOneType {
22     
23     public SpecialOneToOneType(
24             String JavaDoc referencedEntityName,
25             ForeignKeyDirection foreignKeyType,
26             String JavaDoc uniqueKeyPropertyName,
27             boolean lazy,
28             boolean unwrapProxy,
29             String JavaDoc entityName,
30             String JavaDoc propertyName
31     ) {
32         super(
33                 referencedEntityName,
34                 foreignKeyType,
35                 uniqueKeyPropertyName,
36                 lazy,
37                 unwrapProxy,
38                 true,
39                 entityName,
40                 propertyName
41             );
42     }
43     
44     public int getColumnSpan(Mapping mapping) throws MappingException {
45         return super.getIdentifierOrUniqueKeyType(mapping).getColumnSpan(mapping);
46     }
47     
48     public int[] sqlTypes(Mapping mapping) throws MappingException {
49         return super.getIdentifierOrUniqueKeyType(mapping).sqlTypes(mapping);
50     }
51
52     public boolean useLHSPrimaryKey() {
53         return false;
54     }
55     
56     public Object JavaDoc hydrate(ResultSet JavaDoc rs, String JavaDoc[] names, SessionImplementor session, Object JavaDoc owner)
57     throws HibernateException, SQLException JavaDoc {
58         return super.getIdentifierOrUniqueKeyType( session.getFactory() )
59             .nullSafeGet(rs, names, session, owner);
60     }
61     
62     // TODO: copy/paste from ManyToOneType
63

64     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
65     throws HibernateException {
66
67         if ( isNotEmbedded(session) ) {
68             return getIdentifierType(session).disassemble(value, session, owner);
69         }
70         
71         if (value==null) {
72             return null;
73         }
74         else {
75             // cache the actual id of the object, not the value of the
76
// property-ref, which might not be initialized
77
Object JavaDoc id = ForeignKeys.getEntityIdentifierIfNotUnsaved( getAssociatedEntityName(), value, session );
78             if (id==null) {
79                 throw new AssertionFailure(
80                         "cannot cache a reference to an object with a null id: " +
81                         getAssociatedEntityName()
82                 );
83             }
84             return getIdentifierType(session).disassemble(id, session, owner);
85         }
86     }
87
88     public Object JavaDoc assemble(Serializable JavaDoc oid, SessionImplementor session, Object JavaDoc owner)
89     throws HibernateException {
90         //TODO: currently broken for unique-key references (does not detect
91
// change to unique key property of the associated object)
92
Serializable JavaDoc id = (Serializable JavaDoc) getIdentifierType(session).assemble(oid, session, null); //the owner of the association is not the owner of the id
93

94         if ( isNotEmbedded(session) ) return id;
95         
96         if (id==null) {
97             return null;
98         }
99         else {
100             return resolveIdentifier(id, session);
101         }
102     }
103     
104
105
106 }
107
Popular Tags