From: <one...@us...> - 2003-03-29 04:09:22
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl In directory sc8-pr-cvs1:/tmp/cvs-serv947/hibernate/impl Modified Files: SessionFactoryImpl.java SessionImpl.java Log Message: * fixed a bug in SQLExpression * fixed a bug in Expression.ge() * improved proxy handling * fixed problems with select new * reworked import mechanism * added <any> mappings Index: SessionFactoryImpl.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl/SessionFactoryImpl.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** SessionFactoryImpl.java 4 Mar 2003 10:53:46 -0000 1.14 --- SessionFactoryImpl.java 29 Mar 2003 04:08:47 -0000 1.15 *************** *** 93,96 **** --- 93,97 ---- private transient final Map collectionPersisters; private transient final Map namedQueries; + private transient final Map imports; private transient final ConnectionProvider connections; private transient final Properties properties; *************** *** 100,104 **** private transient final Templates templates; private transient final Map querySubstitutions; ! private transient final String[] queryImports; private transient final Dialect dialect; private transient final PreparedStatementCache statementCache; --- 101,105 ---- private transient final Templates templates; private transient final Map querySubstitutions; ! //private transient final String[] queryImports; private transient final Dialect dialect; private transient final PreparedStatementCache statementCache; *************** *** 270,275 **** log.info("Query language substitutions: " + querySubstitutions); ! queryImports = PropertiesHelper.toStringArray(Environment.QUERY_IMPORTS, " ,;:\n\t\r\f", properties); ! if ( queryImports.length!=0 ) log.info( "Query language imports: " + StringHelper.toString(queryImports) ); /*datastore.getNamedQueries().entrySet().iterator(); --- 271,276 ---- log.info("Query language substitutions: " + querySubstitutions); ! //queryImports = PropertiesHelper.toStringArray(Environment.QUERY_IMPORTS, " ,;:\n\t\r\f", properties); ! //if ( queryImports.length!=0 ) log.info( "Query language imports: " + StringHelper.toString(queryImports) ); /*datastore.getNamedQueries().entrySet().iterator(); *************** *** 280,284 **** // TODO: precompile queries }*/ ! namedQueries = cfg.getNamedQueries(); --- 281,286 ---- // TODO: precompile queries }*/ ! namedQueries = new HashMap( cfg.getNamedQueries() ); ! imports = new HashMap( cfg.getImports() ); *************** *** 673,678 **** } ! public String[] getImports() { return queryImports; } --- 675,684 ---- } ! /*public String[] getImports() { return queryImports; + }*/ + public String getImportedClassName(String name) { + String result = (String) imports.get(name); + return (result==null) ? name : result; } Index: SessionImpl.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl/SessionImpl.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** SessionImpl.java 21 Mar 2003 12:03:37 -0000 1.28 --- SessionImpl.java 29 Mar 2003 04:08:47 -0000 1.29 *************** *** 562,566 **** if (obj==null) throw new NullPointerException("attempted to save null"); ! Object object = HibernateProxyHelper.unproxy(obj, this); EntityEntry e = getEntry(object); --- 562,568 ---- if (obj==null) throw new NullPointerException("attempted to save null"); ! if ( !Hibernate.isInitialized(obj) ) throw new PersistentObjectException("uninitialized proxy passed to save()"); ! ! Object object = unproxyAndReassociate(obj); EntityEntry e = getEntry(object); *************** *** 582,586 **** throw new JDBCException("Could not save object", sqle); } ! return doSave(object, obj, id); } --- 584,588 ---- throw new JDBCException("Could not save object", sqle); } ! return doSave(object, id); } *************** *** 594,598 **** if (id==null) throw new NullPointerException("null identifier passed to insert()"); ! Object object = HibernateProxyHelper.unproxy(obj, this); EntityEntry e = getEntry(object); --- 596,602 ---- if (id==null) throw new NullPointerException("null identifier passed to insert()"); ! if ( !Hibernate.isInitialized(obj) ) throw new PersistentObjectException("uninitialized proxy passed to save()"); ! ! Object object = unproxyAndReassociate(obj); EntityEntry e = getEntry(object); *************** *** 610,617 **** } ! doSave(object, obj, id); } ! private Serializable doSave(Object object, Object proxy, Serializable id) throws HibernateException { ClassPersister persister = getPersister(object); --- 614,621 ---- } ! doSave(object, id); } ! private Serializable doSave(Object object, Serializable id) throws HibernateException { ClassPersister persister = getPersister(object); *************** *** 663,667 **** // same object again. QUESTION: should this be done before onSave() is called? // likewise, should it be done before onUpdate()? ! addEntry(object, SAVING, null, id, null, LockMode.WRITE, identityCol, persister); // cascade-save to many-to-one BEFORE the parent is saved --- 667,671 ---- // same object again. QUESTION: should this be done before onSave() is called? // likewise, should it be done before onUpdate()? ! addEntry(object, SAVING, null, id, null, LockMode.WRITE, identityCol, persister); //okay if id is null here (identityCol true) // cascade-save to many-to-one BEFORE the parent is saved *************** *** 708,712 **** addEntry(object, LOADED, values, id, Versioning.getVersion(values, persister), LockMode.WRITE, identityCol, persister); //tableAccesses.add( persister.getQualifiedTableName() ); - if (proxy!=object) proxiesByKey.put(key, proxy); if (!identityCol) insertions.add( new ScheduledInsertion( id, values, object, persister, this ) ); --- 712,715 ---- *************** *** 725,728 **** --- 728,758 ---- } + private void reassociateProxy(Object value) throws MappingException { + HibernateProxy proxy = (HibernateProxy) value; + LazyInitializer li = HibernateProxyHelper.getLazyInitializer(proxy); + reassociateProxy(li, proxy); + } + + private Object unproxyAndReassociate(Object maybeProxy) throws HibernateException { + if ( maybeProxy instanceof HibernateProxy ) { + HibernateProxy proxy = (HibernateProxy) maybeProxy; + LazyInitializer li = HibernateProxyHelper.getLazyInitializer(proxy); + reassociateProxy(li, proxy); + return li.getImplementation(); //initialize + unwrap the object + } + else { + return maybeProxy; + } + } + + private void reassociateProxy(LazyInitializer li, HibernateProxy proxy) throws MappingException { + if ( li.getSession()!=this ) { + ClassPersister persister = getPersister( li.getPersistentClass() ); + Key key = new Key( li.getIdentifier(), persister ); + if ( !proxiesByKey.containsKey(key) ) proxiesByKey.put(key, proxy); // any earlier proxy takes precedence + HibernateProxyHelper.getLazyInitializer( (HibernateProxy) proxy ).setSession(this); + } + } + private void nullifyTransientReferences(Object[] values, Type[] types, boolean earlyInsert, Object self) throws HibernateException { for ( int i=0; i<types.length; i++ ) { *************** *** 826,830 **** if (object==null) throw new NullPointerException("attempted to delete null"); ! object = HibernateProxyHelper.unproxy(object, this); EntityEntry entry = getEntry(object); --- 856,860 ---- if (object==null) throw new NullPointerException("attempted to delete null"); ! object = unproxyAndReassociate(object); EntityEntry entry = getEntry(object); *************** *** 1021,1027 **** if ( value!=null ) { ClassPersister persister = getPersister( ( (EntityType) type ).getPersistentClass() ); ! if ( persister.hasProxy() && (value instanceof HibernateProxy) ) { ! HibernateProxyHelper.getLazyInitializer( (HibernateProxy) value ).setSession(this); ! } } } --- 1051,1055 ---- if ( value!=null ) { ClassPersister persister = getPersister( ( (EntityType) type ).getPersistentClass() ); ! if ( persister.hasProxy() && !Hibernate.isInitialized(value) ) reassociateProxy(value); } } *************** *** 1040,1046 **** if (obj==null) throw new NullPointerException("attempted to update null"); - if ( !Hibernate.isInitialized(obj) ) return; ! Object object = HibernateProxyHelper.unproxy(obj, this); ClassPersister persister = getPersister(object); --- 1068,1078 ---- if (obj==null) throw new NullPointerException("attempted to update null"); ! if ( !Hibernate.isInitialized(obj) ) { ! reassociateProxy(obj); ! return; ! } ! ! Object object = unproxyAndReassociate(obj); ClassPersister persister = getPersister(object); *************** *** 1062,1066 **** } else { ! doUpdate(object, obj, id); } --- 1094,1098 ---- } else { ! doUpdate(object, id); } *************** *** 1076,1081 **** public void saveOrUpdate(Object obj) throws HibernateException { if (obj==null) throw new NullPointerException("attempted to update null"); ! if ( !Hibernate.isInitialized(obj) ) return; ! Object object = HibernateProxyHelper.unproxy(obj, this); EntityEntry e = getEntry(object); --- 1108,1117 ---- public void saveOrUpdate(Object obj) throws HibernateException { if (obj==null) throw new NullPointerException("attempted to update null"); ! ! if ( !Hibernate.isInitialized(obj) ) { ! reassociateProxy(obj); ! return; ! } ! Object object = unproxyAndReassociate(obj); EntityEntry e = getEntry(object); *************** *** 1105,1109 **** else { if ( log.isTraceEnabled() ) log.trace("saveOrUpdate() previously saved instance with id: " + id); ! doUpdate(object, obj, id); } --- 1141,1145 ---- else { if ( log.isTraceEnabled() ) log.trace("saveOrUpdate() previously saved instance with id: " + id); ! doUpdate(object, id); } *************** *** 1134,1144 **** if (id==null) throw new NullPointerException("null is not a valid identifier"); if (obj==null) throw new NullPointerException("attempted to update null"); - if ( !Hibernate.isInitialized(obj) ) return; ! Object object = HibernateProxyHelper.unproxy(obj, this); EntityEntry e = getEntry(object); if (e==null) { ! doUpdate(object, obj, id); } else { --- 1170,1192 ---- if (id==null) throw new NullPointerException("null is not a valid identifier"); if (obj==null) throw new NullPointerException("attempted to update null"); ! if ( obj instanceof HibernateProxy ) { ! Serializable pid = HibernateProxyHelper.getLazyInitializer( (HibernateProxy) obj ).getIdentifier(); ! if ( !id.equals(pid) ) throw new HibernateException( ! "The given proxy had a different identifier value to the given identifier: " + ! pid + "!=" + id ! ); ! } ! ! if ( !Hibernate.isInitialized(obj) ) { ! reassociateProxy(obj); ! return; ! } ! ! Object object = unproxyAndReassociate(obj); EntityEntry e = getEntry(object); if (e==null) { ! doUpdate(object, id); } else { *************** *** 1150,1154 **** } ! private void doUpdate(Object object, Object proxy, Serializable id) throws HibernateException { ClassPersister persister = getPersister(object); --- 1198,1202 ---- } ! private void doUpdate(Object object, Serializable id) throws HibernateException { ClassPersister persister = getPersister(object); *************** *** 1184,1188 **** addEntry(object, LOADED, null, id, persister.getVersion(object), LockMode.NONE, true, persister); //tableAccesses.add( persister.getQualifiedTableName() ); - if (proxy!=object) proxiesByKey.put(key, proxy); cascading++; --- 1232,1235 ---- *************** *** 1358,1362 **** if (lockMode==LockMode.WRITE) throw new HibernateException("Invalid lock mode for lock()"); ! object = HibernateProxyHelper.unproxy(object, this); EntityEntry e = getEntry(object); --- 1405,1411 ---- if (lockMode==LockMode.WRITE) throw new HibernateException("Invalid lock mode for lock()"); ! object = unproxyAndReassociate(object); ! //TODO: if object was an uninitialized proxy, this is inefficient, ! //resulting in two SQL selects EntityEntry e = getEntry(object); *************** *** 1562,1566 **** * Do NOT return a proxy. */ ! public Object immediateLoad(Class clazz, Serializable id) throws SQLException, HibernateException { Object result = doLoad(clazz, id, null, LockMode.NONE, false); throwObjectNotFound(result, id, clazz); --- 1611,1615 ---- * Do NOT return a proxy. */ ! public Object immediateLoad(Class clazz, Serializable id) throws HibernateException { Object result = doLoad(clazz, id, null, LockMode.NONE, false); throwObjectNotFound(result, id, clazz); *************** *** 1643,1647 **** if ( persister.hasProxy() ) { proxy = CGLIBLazyInitializer.getProxy( ! clazz, persister.getProxyInterfaces(), persister.getProxyGetIdentifierMethod(), id, this ); } --- 1692,1700 ---- if ( persister.hasProxy() ) { proxy = CGLIBLazyInitializer.getProxy( ! clazz, ! persister.getProxyInterfaces(), ! persister.getProxyGetIdentifierMethod(), ! id, ! this ); } *************** *** 1773,1786 **** } ! public void refresh(Object object, LockMode lockMode) throws HibernateException { ! if (object==null) throw new NullPointerException("attempted to refresh null"); ! object = HibernateProxyHelper.unproxy(object, this); EntityEntry e = removeEntry(object); if ( !e.existsInDatabase ) throw new HibernateException("this instance does not yet exist as a row in the database"); ! removeEntity( new Key(e.id, e.persister) ); try { e.persister.load( e.id, object, lockMode, this); --- 1826,1844 ---- } ! public void refresh(Object obj, LockMode lockMode) throws HibernateException { ! if (obj==null) throw new NullPointerException("attempted to refresh null"); ! if ( !Hibernate.isInitialized(obj) ) { ! reassociateProxy(obj); ! return; ! } ! Object object = unproxyAndReassociate(obj); EntityEntry e = removeEntry(object); if ( !e.existsInDatabase ) throw new HibernateException("this instance does not yet exist as a row in the database"); ! Key key = new Key(e.id, e.persister); ! removeEntity(key); try { e.persister.load( e.id, object, lockMode, this); *************** *** 2117,2120 **** --- 2175,2179 ---- } + // not for internal use: public Serializable getIdentifier(Object object) throws HibernateException { if (object instanceof HibernateProxy) { *************** *** 2904,2907 **** --- 2963,2974 ---- } + public boolean contains(Object object) { + if (object instanceof HibernateProxy) { + return HibernateProxyHelper.getLazyInitializer( (HibernateProxy) object ).getSession()==this; + } + else { + return entries.containsKey(object); + } + } } |