Announcement Announcement Module
Collapse
No announcement yet.
Spring + hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + hibernate

    hello

    i'm using the latest version of the two framworks Spring & Hibernate to build a dynamique web site so when i want to run the test of constructing the AnnotationSessionFactoryBean i meet this Error

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'annotatedClasses' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
    org.springframework.beans.NotWritablePropertyException: Invalid property 'annotatedClasses' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
            at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:670)
            at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572)
            at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737)
            at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764)
            at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1061)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
            at Main.Main.main(Main.java:22)
    i didn't understand why ?

  • #2
    Can you share Spring config file where you are creating the session factory.

    Comment


    • #3
      Originally posted by rishishehrawat View Post
      Can you share Spring config file where you are creating the session factory.
      hello, thank for your reply
      this is my application context XML config file
      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
      
      <beans>
      
      	<!--
      		========================= Start of PERSISTENCE DEFINITIONS =========================
      	-->
      	
      	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
      		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
      		<property name="url" value="jdbc:mysql://127.0.0.1:3306/pfedb" />
      		<property name="username" value="root" />
      		<property name="password" value="" />
      	</bean>
      
      	<!-- Hibernate SessionFactory Definition -->
      	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      		<property name="annotatedClasses">
            		<list>
      				<value>m.Groupe</value>
            		</list>
            	</property>
      		<property name="hibernateProperties">
      			<props>
      				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
      				<prop key="hibernate.show_sql">true</prop>
      				<prop key="hibernate.format_sql">true</prop>
      			</props>
      		</property>
      		<property name="dataSource" ref="myDataSource" />
      	</bean>
      	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
      </bean>
      	
      </beans>
      and this is my class Main

      Code:
      import org.apache.commons.dbcp.BasicDataSource;
      import org.springframework.beans.factory.xml.XmlBeanFactory;
      import org.springframework.core.io.ClassPathResource;
      import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
      
      public class Main {
      
      	/**
      	 * @param args
      	 */
      	public static void main(String[] args) {
      		// TODO Auto-generated method stub
      		BasicDataSource mds=(BasicDataSource) (new XmlBeanFactory(new ClassPathResource("appco.xml")).getBean("myDataSource"));
      		AnnotationSessionFactoryBean lse= (AnnotationSessionFactoryBean) (new XmlBeanFactory(new ClassPathResource("appco.xml")).getBean("sessionFactory"));
      
      		System.out.println("Hello");
      	}
      
      }
      Blacky

      Comment


      • #4
        The session facrory definition seems fine. Can you check if the correct config file (that you shared with me) is picked up by your program. There could be 2 configs file with same name in the class path.

        Comment


        • #5
          Originally posted by rishishehrawat View Post
          The session facrory definition seems fine. Can you check if the correct config file (that you shared with me) is picked up by your program. There could be 2 configs file with same name in the class path.
          hello
          no, there is just one config file (that i shared it with you) with the same name that you saw it in the Main class and, i put it in the src folder.
          I don't know what's wrong.
          ----------
          Blacky

          Comment


          • #6
            hello

            can anyone help me ?

            Comment


            • #7
              Hi,

              is m.Groupe the full package path of class Groupe?

              Comment


              • #8
                Originally posted by boban View Post
                Hi,

                is m.Groupe the full package path of class Groupe?
                yes is the full package and this is the source code of this class

                Code:
                package m;
                
                import java.io.Serializable;
                import java.util.regex.Matcher;
                import java.util.regex.Pattern;
                
                import javax.persistence.*;
                
                @Entity 
                @Table(name = "groupe")
                public class Groupe {
                	
                	@Id
                	private AlphaNumericId groupeID;
                	//////////
                	///////////////////
                	public void setGroupeID(String groupeID){
                		this.groupeID.setId(groupeID);
                	}
                	public String getGroupeID(){
                		return this.groupeID.getId();
                	}
                	////////////////////
                	public Groupe(String groupeID){
                		this.groupeID=new AlphaNumericId(groupeID);
                	}
                	private Groupe(){
                		
                	}
                	
                	// --------------------------------------------------------------
                	public String getNextGroupeID(){
                		return this.groupeID.getNextId();
                	}
                	public String getPreviousGroupeID(){
                		return this.groupeID.getPreviousId();
                	}
                	// --------------------------------------------------------------
                	
                	// ---------------------------------------------------------------
                	@Embeddable
                	class AlphaNumericId implements Serializable{
                		
                		@Column(name = "groupeID",columnDefinition = "String")
                		private String id;
                		private char[] digits={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
                
                		public AlphaNumericId(String id){
                			this.setId(id);
                		}
                		
                	 	public void setId(String id){
                			Pattern p= Pattern.compile("([a-z]|[A-Z]|[0-9])+");
                			Matcher m = p.matcher(id);
                			if(m.matches()){
                				this.id=id;
                			}else{
                			}
                		}
                		
                		public String getId(){
                			return id;
                		}
                		
                		// générateur d'id
                		public String getNextId(){
                			char[] idc=id.toCharArray();
                			int i=idc.length-1;
                			char ct=this.getNextDigit(idc[i]);
                			while(ct=='0'&&i>0){
                				idc[i]=ct;
                				i--;
                				idc[i]=this.getNextDigit(idc[i]);
                				ct=this.getNextDigit(idc[i]);
                			}
                			return idc.toString();
                		}
                		
                		public String getPreviousId(){
                			char[] idc=id.toCharArray();
                			int i=idc.length-1;
                			char ct=this.getPreviousDigit(idc[i]);
                			while(ct=='Z'&&i>0){
                				idc[i]=ct;
                				i--;
                				idc[i]=this.getPreviousDigit(idc[i]);
                				ct=this.getPreviousDigit(idc[i]);
                			}
                			return idc.toString();
                		}
                		
                		private char getNextDigit(char ch){
                			if(ch !='Z'){
                				int i=0;
                				while(ch!=digits[i]&&i<digits.length){
                					i++;
                				}
                				return digits[i++];
                			}else{
                				return '0';
                			}
                		}
                		 
                		private char getPreviousDigit(char ch){
                			if(ch != '0'){
                				int i=0;
                				while(ch!=digits[i]&&i<digits.length){
                					i++;
                				}
                				return digits[i--];
                			}else{
                				return 'Z';
                			}
                		}
                	}
                	
                }
                Thank you for your repply

                --------------
                Blacky
                Last edited by BlackBeard; Aug 27th, 2011, 02:11 PM.

                Comment


                • #9
                  can any one help me ?!!!!!!!!!!!!!!!!!!!!!!!!

                  Comment


                  • #10
                    Pleazzzzzzzzzzzzzzzzzzzz can any one help me
                    i still waiting for the response, i can't continue without resolving this problem
                    plz help me

                    Comment


                    • #11
                      Hi BlackBeard

                      in Groupe class

                      1. constructor method has to be public.

                      remove the black constructor method from your Group class.
                      give try.

                      ===========================
                      remove this ->
                      private Groupe(){

                      }
                      ===========================

                      Placidus.

                      Comment


                      • #12
                        Hello

                        I change all of the Groupe class code with this code, and i still have the same problem

                        Code:
                        package m;
                        // default package
                        // Generated 28 août 2011 02:18:20 by Hibernate Tools 3.4.0.CR1
                        
                        import javax.persistence.Column;
                        import javax.persistence.Entity;
                        import javax.persistence.Id;
                        import javax.persistence.Table;
                        
                        /**
                         * Groupe generated by hbm2java
                         */
                        @Entity
                        @Table(name = "groupe", catalog = "pfedb")
                        public class Groupe implements java.io.Serializable {
                        
                        	private String groupeId;
                        	
                        	public Groupe(){
                        		
                        	}
                        	
                        	public Groupe(String groupeId) {
                        		this.groupeId = groupeId;
                        	}
                        
                        	@Id
                        	@Column(name = "groupeID", unique = true, nullable = false)
                        	public String getGroupeId() {
                        		return this.groupeId;
                        	}
                        
                        	public void setGroupeId(String groupeId) {
                        		this.groupeId = groupeId;
                        	}
                        
                        }
                        Code:
                        Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [appco.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'annotatedClasses' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
                        org.springframework.beans.NotWritablePropertyException: Invalid property 'annotatedClasses' of bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
                        	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:670)
                        	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:572)
                        	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:737)
                        	at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:764)
                        	at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:753)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1061)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:857)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:378)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:233)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:145)
                        	at m.Main.main(Main.java:17)
                        plz help me i'm using th Spring 3.1 version
                        and this is the list of all the jars that i use for this project, there are all in the class path of this project

                        Code:
                        commons-codec-1.4.jar
                        commons-io-1.4.jar
                        commons-lang-2.4.jar
                        hibernate-jpa-2.0-api-1.0.0.Final.jar
                        hibernate-search-analyzers-3.4.0.Final.jar
                        i18nlog-1.0.10.jar
                        infinispan-core-4.2.1.FINAL.jar
                        infinispan-lucene-directory-4.2.1.FINAL.jar
                        jboss-transaction-api-1.0.1.GA.jar
                        jgroups-2.12.0.Final.jar
                        jgroups-2.8.0.GA.jar
                        log4j-1.2.16.jar
                        lucene-analyzers-3.1.0.jar
                        marshalling-api-1.2.3.GA.jar
                        rhq-pluginAnnotations-3.0.1.jar
                        river-1.2.3.GA.jar
                        slf4j-log4j12-1.6.1.jar
                        solr-analysis-extras-3.1.0.jar
                        solr-commons-csv-3.1.0.jar
                        solr-core-3.1.0.jar
                        solr-solrj-3.1.0.jar
                        jms-1.1.jar
                        jsr250-api-1.0.jar
                        jta-1.1.jar
                        antlr-2.7.6.jar
                        commons-collections-3.1.jar
                        commons-lang-2.4.jar
                        dom4j-1.6.1.jar
                        hibernate-commons-annotations-3.2.0.Final.jar
                        hibernate-core-3.6.3.Final.jar
                        lucene-core-3.1.0.jar
                        slf4j-api-1.6.1.jar
                        hibernate-search-3.4.0.Final.jar
                        hibernate-search-infinispan-3.4.0.Final.jar
                        javassist.jar
                        mysql-connector-java-5.1.17-bin.jar
                        org.springframework.aop-3.1.0.M2.jar
                        org.springframework.asm-3.1.0.M2.jar
                        org.springframework.aspects-3.1.0.M2.jar
                        org.springframework.beans-3.1.0.M2.jar
                        org.springframework.context-3.1.0.M2.jar
                        org.springframework.context.support-3.1.0.M2.jar
                        org.springframework.core-3.1.0.M2.jar
                        org.springframework.expression-3.1.0.M2.jar
                        org.springframework.instrument-3.1.0.M2.jar
                        org.springframework.instrument.tomcat-3.1.0.M2.jar
                        org.springframework.jdbc-3.1.0.M2.jar
                        org.springframework.jms-3.1.0.M2.jar
                        org.springframework.orm-3.1.0.M2.jar
                        org.springframework.oxm-3.1.0.M2.jar
                        org.springframework.test-3.1.0.M2.jar
                        org.springframework.transaction-3.1.0.M2.jar
                        org.springframework.web-3.1.0.M2.jar
                        org.springframework.web.portlet-3.1.0.M2.jar
                        org.springframework.web.servlet-3.1.0.M2.jar
                        org.springframework.web.struts-3.1.0.M2.jar
                        Thank you

                        i'm waiting for the response
                        ---------------
                        Blacky

                        Comment


                        • #13
                          1. remove the Groupe -> constructor method.
                          2. remane the class to some thing else , this will make sure it's accessing new class -> like Groupe2

                          give try.

                          Comment


                          • #14
                            I'd start by using the correct headers on your xml. You say you're using the latest Spring version (I suppose 3.0.5 or 3.1) but you use the dtd on the header which is deprecated since Spring 2. This is the proper way to start your applicationContext.xml:

                            Code:
                            <?xml version="1.0" encoding="UTF-8"?>
                            <beans xmlns="http://www.springframework.org/schema/beans" 
                            	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                   xmlns:context="http://www.springframework.org/schema/context"
                                   xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                   					   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                        			   http://www.springframework.org/schema/context 
                                        			   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
                            Also, check that the Spring version and Hibernate version used are in fact compatible (and I suggest using Maven to resolve both framework dependencies).

                            Comment


                            • #15
                              Hello
                              thanks for the reply

                              I use the Eclipse INDIGO, the hibernate-search-3.4.0.Final-dist.zip and spring-framework-3.1.0.M2.zip

                              I resolve the problem, i'm soory because i had put another old version of the Spring in the C:\Program Files\Java\jre6\lib\ext folder i forgot that
                              so i remove all of their old jars

                              I still have this problem

                              Code:
                              Exception in thread "main" java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
                              	at m.Main.main(Main.java:18)
                              and thank you very much
                              Last edited by BlackBeard; Aug 31st, 2011, 02:28 AM.

                              Comment

                              Working...
                              X