Bläddra i källkod

Hibernate 3 refactorings.

Ben Alex 20 år sedan
förälder
incheckning
a2d68e4fc5

+ 1 - 1
domain/project.xml

@@ -15,7 +15,7 @@
     <dependency>
       <groupId>hibernate</groupId>
       <artifactId>hibernate</artifactId>
-      <version>2.1.8</version>
+      <version>3.0rc1</version>
       <type>jar</type>
     </dependency>
     <dependency>

+ 23 - 29
domain/src/main/java/org/acegisecurity/domain/hibernate/DaoHibernate.java

@@ -15,35 +15,32 @@
 
 package net.sf.acegisecurity.domain.hibernate;
 
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+
 import net.sf.acegisecurity.domain.PersistableEntity;
 import net.sf.acegisecurity.domain.dao.Dao;
 import net.sf.acegisecurity.domain.dao.EvictionCapable;
 import net.sf.acegisecurity.domain.dao.PaginatedList;
 
-import net.sf.hibernate.Criteria;
-import net.sf.hibernate.Hibernate;
-import net.sf.hibernate.HibernateException;
-import net.sf.hibernate.Session;
-import net.sf.hibernate.expression.Expression;
-import net.sf.hibernate.expression.MatchMode;
-import net.sf.hibernate.expression.Order;
-import net.sf.hibernate.metadata.ClassMetadata;
-import net.sf.hibernate.type.Type;
-
-import org.springframework.orm.hibernate.HibernateCallback;
-import org.springframework.orm.hibernate.HibernateObjectRetrievalFailureException;
-import org.springframework.orm.hibernate.support.HibernateDaoSupport;
-
+import org.hibernate.Criteria;
+import org.hibernate.EntityMode;
+import org.hibernate.Hibernate;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.criterion.Expression;
+import org.hibernate.criterion.MatchMode;
+import org.hibernate.criterion.Order;
+import org.hibernate.metadata.ClassMetadata;
+import org.hibernate.type.Type;
+import org.springframework.orm.hibernate3.HibernateCallback;
+import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 import org.springframework.util.Assert;
 
-import java.io.Serializable;
-
-import java.util.Collection;
-import java.util.List;
-
 
 /**
- * {@link Dao} implementation that uses Hibernate for persistence.
+ * {@link Dao} implementation that uses Hibernate 3 for persistence.
  *
  * @author Ben Alex
  * @author Matthew Porter
@@ -103,13 +100,8 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
 
     public PersistableEntity readId(Serializable id) {
         Assert.notNull(id);
-
-        try {
-            return (PersistableEntity) getHibernateTemplate().load(supportsClass,
-                id);
-        } catch (HibernateObjectRetrievalFailureException notFound) {
-            return null;
-        }
+		
+        return (PersistableEntity) getHibernateTemplate().get(supportsClass, id);
     }
 
     public PaginatedList scroll(PersistableEntity value, int firstElement,
@@ -215,7 +207,9 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
                     /* for each persistent property of the bean */
                     for (int i = 0; i < propertyNames.length; i++) {
                         String name = propertyNames[i];
-                        Object value = classMetadata.getPropertyValue(bean, name);
+                        
+                        // TODO: Check if EntityMode.POJO appropriate
+                        Object value = classMetadata.getPropertyValue(bean, name, EntityMode.POJO);
 
                         if (value == null) {
                             continue;
@@ -231,7 +225,7 @@ public class DaoHibernate extends HibernateDaoSupport implements Dao,
                         }
 
                         // ignore any collections
-                        if (propertyTypes[i].isPersistentCollectionType()) {
+                        if (propertyTypes[i].isCollectionType()) {
                             continue;
                         }
 

+ 36 - 7
domain/src/main/java/org/acegisecurity/domain/hibernate/IntrospectionManagerHibernate.java

@@ -16,18 +16,24 @@
 package net.sf.acegisecurity.domain.hibernate;
 
 import net.sf.acegisecurity.domain.validation.IntrospectionManager;
+import net.sf.acegisecurity.domain.validation.ValidationRegistryManager;
 
-import net.sf.hibernate.HibernateException;
-import net.sf.hibernate.SessionFactory;
-import net.sf.hibernate.metadata.ClassMetadata;
-import net.sf.hibernate.type.Type;
+import org.hibernate.EntityMode;
+import org.hibernate.HibernateException;
+import org.hibernate.SessionFactory;
+
+import org.hibernate.metadata.ClassMetadata;
+
+import org.hibernate.type.Type;
 
 import org.springframework.beans.factory.InitializingBean;
 
-import org.springframework.orm.hibernate.HibernateSystemException;
+import org.springframework.orm.hibernate3.HibernateSystemException;
 
 import org.springframework.util.Assert;
 
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 
 
@@ -57,6 +63,7 @@ public class IntrospectionManagerHibernate implements IntrospectionManager,
     //~ Instance fields ========================================================
 
     private SessionFactory sessionFactory;
+    private ValidationRegistryManager validationRegistryManager;
 
     //~ Methods ================================================================
 
@@ -68,8 +75,27 @@ public class IntrospectionManagerHibernate implements IntrospectionManager,
         return this.sessionFactory;
     }
 
+    public void setValidationRegistryManager(
+        ValidationRegistryManager validationRegistryManager) {
+        this.validationRegistryManager = validationRegistryManager;
+    }
+
+    public ValidationRegistryManager getValidationRegistryManager() {
+        return validationRegistryManager;
+    }
+
     public void afterPropertiesSet() throws Exception {
+        Assert.notNull(validationRegistryManager,
+            "ValidationRegistryManager is required");
         Assert.notNull(sessionFactory, "SessionFactory is required");
+
+        // Eagerly pre-register Validators for all Hibernate metadata-defined classes
+        Collection mappedClasses = this.sessionFactory.getAllClassMetadata()
+                                                      .keySet();
+
+        for (Iterator iter = mappedClasses.iterator(); iter.hasNext();) {
+            this.validationRegistryManager.findValidator((Class) iter.next());
+        }
     }
 
     public void obtainImmediateChildren(Object parentObject, List allObjects) {
@@ -90,9 +116,12 @@ public class IntrospectionManagerHibernate implements IntrospectionManager,
                 for (int i = 0; i < propertyNames.length; i++) {
                     Type propertyType = classMetadata.getPropertyType(propertyNames[i]);
 
-                    if (propertyType.isObjectType()) {
+                    // Add this property to the List of Objects to validate
+                    // only if a Validator is registered for that Object
+                    if (this.validationRegistryManager.findValidator(
+                            propertyType.getReturnedClass()) != null) {
                         allObjects.add(classMetadata.getPropertyValue(
-                                parentObject, propertyNames[i]));
+                                parentObject, propertyNames[i], EntityMode.POJO));
                     }
                 }
             }