Announcement Announcement Module
Collapse
No announcement yet.
Error when use Spring and Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error when use Spring and Hibernate

    When i use Hibernate and not use Spring then no error occur my program.
    But when i use Spring then error occur.
    my error

    test:
    [junit] ERROR - Configuration.add(252) | Could not compile the mapping document
    [junit] net.sf.hibernate.PropertyNotFoundException: field not found: primaryKey
    [junit] at net.sf.hibernate.property.DirectPropertyAccessor.g etField(DirectPropertyAccessor.java:74)
    [junit] at net.sf.hibernate.property.DirectPropertyAccessor.g etField(DirectPropertyAccessor.java:80)
    [junit] at net.sf.hibernate.property.DirectPropertyAccessor.g etField(DirectPropertyAccessor.java:80)
    [junit] at net.sf.hibernate.property.DirectPropertyAccessor.g etGetter(DirectPropertyAccessor.java:88)
    [junit] at net.sf.hibernate.util.ReflectHelper.getter(Reflect Helper.java:81)
    [junit] at net.sf.hibernate.util.ReflectHelper.reflectedPrope rtyClass(ReflectHelper.java:90)
    [junit] at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.j ava:286)
    [junit] at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1 243)
    [junit] at net.sf.hibernate.cfg.Configuration.add(Configurati on.java:249)
    [junit] at net.sf.hibernate.cfg.Configuration.addInputStream( Configuration.java:285)
    [junit] at org.springframework.orm.hibernate.LocalSessionFact oryBean.afterPropertiesSet(LocalSessionFactoryBean .java:322)
    [junit] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:801)
    [junit] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:249)
    [junit] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:177)
    [junit] at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:159)
    [junit] at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:177)
    [junit] at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:268)
    [junit] at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:68)
    [junit] at net.luvina.framework.core.test.dao.BaseDAOTestCase .<init>(BaseDAOTestCase.java:20)
    [junit] at net.luvina.useraccount.dao.DepartmentDAOTest.<init >(DepartmentDAOTest.java:20)
    [junit] at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    [junit] at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    [junit] at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    [junit] at java.lang.reflect.Constructor.newInstance(Construc tor.java:274)
    [junit] at junit.framework.TestSuite.createTest(TestSuite.jav a:131)
    [junit] at junit.framework.TestSuite.addTestMethod(TestSuite. java:114)
    [junit] at junit.framework.TestSuite.<init>(TestSuite.java:75 )
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.<init>(JUnitTestRunner.java:225)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.<init>(JUnitTestRunner.java:177)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.launch(JUnitTestRunner.java:651)
    [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnit TestRunner.main(JUnitTestRunner.java:558)
    [junit] Testsuite: net.luvina.useraccount.dao.DepartmentDAOTest
    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.016 sec

    file hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
    <class name="net.luvina.useraccount.model.User" table="User">
    <id name="USERID" type="java.lang.Long">
    <generator class="increment" />
    </id>
    <property name="NAME" />
    <property name="DEPTID" />
    </class>
    <class name="net.luvina.useraccount.model.Department" table="Department">
    <id name="DEPTID" type="java.lang.Long">
    <generator class="increment" />
    </id>
    <property name="NAME" />
    </class>
    <class name="net.luvina.useraccount.model.Test" table="Test">
    <id name="ID" type="java.lang.Long">
    <generator class="increment" />
    </id>
    <property name="NAME" />
    </class>
    <class name="net.luvina.useraccount.model.Test1" table="Test1">
    <composite-id name="primaryKey" class="net.luvina.useraccount.model.Test1PK">
    <key-property name="ID" />
    <key-property name="ID1" />
    </composite-id>
    <property name="NAME" />
    </class>
    </hibernate-mapping>

    file applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>org.hsqldb.jdbcDrive r</value></property>
    <property name="url"><value>jdbc:hsqldb:db/testframework</value></property>
    <property name="username"><value>sa</value></property>
    <!-- Make sure <value> tags are on same line - if they're not,
    authentication will fail -->
    <property name="password"><value></value></property>
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>net/luvina/useraccount/xml/hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.H SQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
    </props>
    </property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <!-- User -->
    <bean id="userDAO" class="net.luvina.useraccount.dao.impl.UserDAOImpl ">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="userHomeTarget" class="net.luvina.useraccount.service.impl.UserHom eImpl">
    <property name="userDAO"><ref local="userDAO"/></property>
    </bean>
    <bean id="userHome" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="userHomeTarget"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>
    <!-- Department -->
    <bean id="departmentDAO" class="net.luvina.useraccount.dao.impl.DepartmentD AOImpl">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="departmentHomeTarget" class="net.luvina.useraccount.service.impl.Departm entHomeImpl">
    <property name="departmentDAO"><ref local="departmentDAO"/></property>
    </bean>
    <bean id="departmentHome" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="departmentHomeTarget"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>
    <!-- Test -->
    <bean id="testDAO" class="net.luvina.useraccount.dao.impl.TestDAOImpl ">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="testHomeTarget" class="net.luvina.useraccount.service.impl.TestHom eImpl">
    <property name="testDAO"><ref local="testDAO"/></property>
    </bean>
    <bean id="testHome" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="testHomeTarget"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>
    <!-- Test1 -->
    <bean id="test1DAO" class="net.luvina.useraccount.dao.impl.Test1DAOImp l">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="test1HomeTarget" class="net.luvina.useraccount.service.impl.Test1Ho meImpl">
    <property name="test1DAO"><ref local="test1DAO"/></property>
    </bean>
    <bean id="test1Home" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="test1HomeTarget"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>
    </beans>

    I had wrong? please tell me.Thanks for reading!

  • #2
    In the composite-id loose the name=... attribute, or define a getter/setter for the primaryKey property.

    Name of the composite key is optional.

    Comment

    Working...
    X