7 Replies Latest reply on Mar 1, 2011 8:15 AM by jaikiran

    JBoss 6 + Hibernate Validator 4

    rajan11

      According to the release notes Bean Validation 4.1.0 should be supported:

      [ JBAS-8699 ]                 Upgrade Bean Validation to 4.1.0.Final

       

      But I see this when I start my server:

      01:24:11,618 INFO  [Version] Hibernate Validator 3.1.0.GA

       

      Am I missing something?

       

      Regards

        • 1. Re: JBoss 6 + Hibernate Validator 4
          jaikiran

          Are you packaging any hibernate related jars in your application? The hibernate-validator.jar in JBOSS_HOME/common/lib is of 4.1.0.Final version

          • 2. Re: JBoss 6 + Hibernate Validator 4
            rajan11

            Jaikiran,

             

            Thanks for the response. After I removed all hibernate realted jars from my war file, I can see Validator 4 is being used.

             

            but I get this error:

            servlet cache.services threw exception: java.lang.IllegalArgumentException: Not an entity: class com.nexage.admin.core.model.Site
            at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160) [:3.6.0.Final]
            at org.hibernate.ejb.criteria.QueryStructure.from(QueryStructure.java:138) [:3.6.0.Final]
            at org.hibernate.ejb.criteria.CriteriaQueryImpl.from(CriteriaQueryImpl.java:179) [:3.6.0.Final]
            at com.nexage.admin.core.dao.jpa.GenericJpaDao.getCriteria(GenericJpaDao.java:197) [:]
            at com.nexage.admin.core.dao.jpa.GenericJpaDao.findByAttribute(GenericJpaDao.java:151) [:]
            at com.nexage.admin.core.manager.impl.SiteManagerImpl.getNewerVersion(SiteManagerImpl.java:62) [:]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_22]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_22]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_22]
            at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_22]
            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) [:3.0.4.RELEASE]
            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [:3.0.4.RELEASE]

            servlet cache.services threw exception: java.lang.IllegalArgumentException: Not an entity: class com.nexage.admin.core.model.Site

            at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160) [:3.6.0.Final]

            at org.hibernate.ejb.criteria.QueryStructure.from(QueryStructure.java:138) [:3.6.0.Final]

            at org.hibernate.ejb.criteria.CriteriaQueryImpl.from(CriteriaQueryImpl.java:179) [:3.6.0.Final]

            at com.nexage.admin.core.dao.jpa.GenericJpaDao.getCriteria(GenericJpaDao.java:197) [:]

            at com.nexage.admin.core.dao.jpa.GenericJpaDao.findByAttribute(GenericJpaDao.java:151) [:]

            at com.nexage.admin.core.manager.impl.SiteManagerImpl.getNewerVersion(SiteManagerImpl.java:62) [:]

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_22]

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_22]

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_22]

            at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_22]

            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) [:3.0.4.RELEASE]

            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [:3.0.4.RELEASE]

            My call is properly annotated using @Entity annotations.
            Do I have to add all my entities in persistence.xml?
            Cheers,
            Rajan

            • 3. Re: JBoss 6 + Hibernate Validator 4
              jaikiran

              You'll have to provide more details on how your application is packaged and what all it contains.

              • 4. Re: JBoss 6 + Hibernate Validator 4
                rajan11

                First of all, thank you very much for your time and help offered so far. I really appreciate it.

                Here are the details of my application:

                 

                I am using Spring 3.0.4 + JPA 2 (Using Hibernate Provider) in my application.

                The application is devided in to two coponents:

                1. 1. Data Access Component : Defines all my entities (+ my DAOs and Managers). This is packaged as a JAR. Spring application contexts XMLs are bundled in this JAR. JPA related configuration is shown below:
                  • applicationContext.xml
                    • <!-- Externalize application properties -->
                          <context:property-placeholder location="classpath*:/core.properties" ignore-unresolvable="true"/>
                          
                          <!-- 
                              Datasource definition is placed in a separate context file so that
                              unit tests can use its own datasource
                           -->
                          <bean id="dataSource"
                              class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
                              p:driverClassName="${core.db.driver}"
                              p:url="${core.db.url}" 
                              p:username="${core.db.username}"
                              p:password="${core.db.password}" />
                      
                      <!-- Enable JPA Support -->
                          <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
                      
                          <!-- Define EntityManagerFactory and Datasource -->
                          <!-- By default LocalContainerEntityManagerFactoryBean supports standard JPA 
                               by searching persistence.xml file. By default it tries to load the
                               persistence.xml file from classpath:META-INF/persistence.xml. 
                               
                               persistence.xml file is also used to enable 2nd level caching for
                               Hibernate.
                               
                               Configure
                               org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager
                               if you want to customize this.
                          -->
                          
                          <util:map id="jpaPropertyMap" map-class="java.util.TreeMap">
                              <entry key="javax.persistence.validation.factory" value-ref="validator"/>
                          </util:map>
                          
                          <bean id="jpaAdapter"
                              class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                              p:showSql="false"
                              p:generateDdl="false"/>
                              
                          <bean id="jpaDialect"
                                class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
                          
                          <bean id="entityManagerFactory"
                              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
                              p:dataSource-ref="dataSource"
                              p:persistenceUnitName="${persistence.unitname}"
                              p:jpaDialect-ref="jpaDialect"
                              p:jpaPropertyMap-ref="jpaPropertyMap"
                              p:jpaVendorAdapter-ref="jpaAdapter">
                          </bean>
                              
                          <!-- Define Transaction Manager -->
                          <bean id="transactionManager"
                                class="org.springframework.orm.jpa.JpaTransactionManager"
                                p:nestedTransactionAllowed="true"
                                p:entityManagerFactory-ref="entityManagerFactory" />
                      
                          <!-- enable the configuration of transactional behavior based on annotations -->
                          <tx:annotation-driven transaction-manager="transactionManager" />
                          
                          <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
                      
                  • persistence.xml
                    • <?xml version="1.0" encoding="UTF-8"?>
                      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                                       http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                                   version="2.0" >
                          <persistence-unit name="ApplicationEntityManager" transaction-type="RESOURCE_LOCAL">
                              <provider>org.hibernate.ejb.HibernatePersistence</provider>
                              <non-jta-data-source>java:/coreDS</non-jta-data-source>
                              <properties>
                                  <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
                                  <property name="hibernate.cache.provider_configuration"    value="/ehcache.xml" />
                                  <property name="hibernate.cache.use_second_level_cache" value="true" />
                                  <property name="hibernate.cache.use_query_cache" value="true" />
                                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                                  <property name="hibernate.archive.autodetection" value="class"/> 
                                   
                                   <!-- If enabled, the session will be automatically flushed during 
                                      the before completion phase of the transaction. --> 
                                  
                                  <property name="hibernate.transaction.flush_before_completion" value="false"/>
                              </properties>        
                          </persistence-unit>
                      
                    • Entity is defined as below:
                      • @Entity 
                        @Table(name = "site")
                        @NamedQueries({
                            @NamedQuery(name="getNewerVersion",
                                      query="SELECT site FROM Site site WHERE site.id = :siteId and site.version > :version")
                        })
                        public class Site extends BaseModel implements Serializable {
                        
                            private static final long serialVersionUID = 6033482787642156697L;
                            
                            public Site() {
                                // Default constructor is required for JPA.
                                // Protected allows some protection in terms of new entity creation
                            }
                           
                        
                        
                    • BaseModel :
                      • @MappedSuperclass
                        public abstract class BaseModel implements Serializable{
                            
                            @GeneratedValue(strategy=GenerationType.AUTO)
                            @Column(nullable=false)
                            private Long pid;
                            
                            @Version
                            @Column(name="VERSION", nullable=false)
                            private int version;
                        
                  • 2. Services Component: Defines the REST based Services using Spring 3.0  REST support. This component uses the above mentioned JAR to access DB.  JAR is bundled in WEB-INF/lib in the WAR.
                    • web.xml
                      • <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                                 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
                                 version="2.4"> 
                        
                            <context-param>
                                  <param-name>contextConfigLocation</param-name>
                                  <param-value>WEB-INF/applicationContext*.xml</param-value>
                            </context-param>
                        
                            <listener>
                                  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                            </listener>
                        
                        
                            <servlet>
                                <servlet-name>cache.services</servlet-name>
                                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                                 <load-on-startup>1</load-on-startup>
                            </servlet>
                            
                             <servlet-mapping>
                                <servlet-name>cache.services</servlet-name>
                                <url-pattern>/</url-pattern>
                            </servlet-mapping>
                        </web-app>
                        
                    • applicationContext-services.xml
                      •     <!-- enabling annotation driven configuration /-->
                            <context:annotation-config/>
                            <!-- Externalize application properties -->
                            <context:property-placeholder location="classpath:app.properties"/>
                        
                            <!-- Loads application context from the DB JAR -->
                            <import resource="classpath*:/applicationContext.xml"/>
                        

                  The WAR packages the JAR in the WEB-INF/lib. All spring dependencies are packaged as well. The WAR doesn't include any of the hibernate dependencies (as suggested by you earlier).

                   

                  Hope this is sufficient information. Let me know if you need any other details.

                   

                  Cheers,

                  Rajan

                  • 5. Re: JBoss 6 + Hibernate Validator 4
                    4bugzilla

                    Rajan,

                     

                    Have you been able to solve this problem? I'm having exactly same issue and for now no clue how to fix this.

                     

                    Thanks,

                    Serhiy

                    • 6. Re: JBoss 6 + Hibernate Validator 4
                      rajan11

                      Try including all your entities in persistence.xml. For me, JBoss doesn't work otherwise.

                      I think that fixed my issues.

                      • 7. Re: JBoss 6 + Hibernate Validator 4
                        jaikiran

                        It should have worked without explicitly adding those class names in the persistence.xml. Are you packaging any jar file(s) containing javax.persistence.* classes in your application? If yes, try removing those jar(s) from the application packaging and see if that helps.