My project is using hibernate3 along with spring 2.5. Now I am migrating it to Spring5 and hibernate5 with JPA.
I have hbm.xml for my entities using hibernate-mapping. After migration I get the error:
Caused by: org.hibernate.AssertionFailure: Unable to locate referenced
entity mapping [com.mycompany.common.admin.Entity1] in order to
process many-to-one FK : com.mycompany.common.admin.Entity2
The Entity2 has many-to-one FK with reference of Entity1
The code for entitymanagerfactory in applicationContext.xml:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
<property name="packagesToScan" value="com.mycompany"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="persistenceUnitPostProcessors">
<list>
<bean
class="org.springframework.data.jpa.support.ClasspathScanningPersistenceUnitPostProcessor">
<constructor-arg index="0" value="com.mycompany" />
<property name="mappingFileNamePattern" value="classpath*:com/**/*hbm.xml" />
</bean>
</list>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
The hbm.xml for Entity2 is as below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mycompany.common.admin.Entity2" table="entity2">
<id column="id" name="ID">
<generator class="uuid"/>
</id>
<property name="name" type="string">
<column name="name"/>
</property>
<many-to-one class="com.mycompany.common.admin.Entity1" name="entity1" not-null="true"/>
</class>
</hibernate-mapping>
Error stack trace:
Caused by: org.hibernate.AssertionFailure: Unable to locate referenced entity mapping [com.mycompany.common.admin.Entity1] in order to process many-to-one FK : com.mycompany.common.admin.Entity2.entity1
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$ManyToOneColumnBinder.doSecondPass(ModelBinder.java:4066)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1696)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1652)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
Please let me know what am I missing here.
Related
can you please help me figure out what is wrong with the DB_VENDOR in MyBatis? I'm using mybatis 3.4.2, mybatis-spring 1.3.1, spring 4.3.6.RELEASE.
Everything, but the vendor dependent queries, works fine. The example query is not vendor dependent, this is only for the sake of this example. If I remove the databaseId attribute from it and remove the duplicated one, this query works.
I need to support Oracle and PostgreSQL and have already checked the metadata getDatabaseProductName() directly within my test scope, getting "Oracle" and "PostgreSQL". So if I got the documentation right, it should work, shouldn't it?
All I get with the databaseId in place is:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): mappers.ConfigurationMapper.containsKey
test-mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "Http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<databaseIdProvider type="DB_VENDOR">
<property name="PostgreSQL" value="postgres"/>
<property name="Oracle" value="oracle"/>
</databaseIdProvider>
<mappers>
<mapper resource="mybatis/ConfigurationMapper.xml"/>
</mappers>
</configuration>
test-context.xml (having another one with PostgreSQL datasource)
<mybatis:scan base-package="mappers" annotation="org.apache.ibatis.annotations.Mapper" factory-ref="sqlSessionFactory"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:XE"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:test-mybatis-config.xml"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
ConfigurationMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mappers.ConfigurationMapper">
<select id="containsKey" resultType="string" parameterType="string" databaseId="oracle">
SELECT '1' AS test FROM config_keys WHERE key = #{configKey}
</select>
<select id="containsKey" resultType="string" parameterType="string" databaseId="postgres">
SELECT '1' AS test FROM config_keys WHERE key = #{configKey}
</select>
</mapper>
mapper interface:
#Mapper
public interface ConfigurationMapper {
String containsKey(#Param("configKey") String configKey);
}
Found it. The answer is in the XMLConfigBuilder:
Environment environment = configuration.getEnvironment();
if (environment != null && databaseIdProvider != null) {
String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
configuration.setDatabaseId(databaseId);
}
The environment must be configured in order to make the databaseId work.
Here is how to use multi-db support in spring environment: http://www.mybatis.org/spring/factorybean.html
<bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="SQL Server">sqlserver</prop>
<prop key="DB2">db2</prop>
<prop key="Oracle">oracle</prop>
<prop key="MySQL">mysql</prop>
</props>
</property>
</bean>
<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
<property name="properties" ref="vendorProperties"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
<property name="databaseIdProvider" ref="databaseIdProvider"/>
</bean>
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'studentDao' while setting bean property 'studentDao';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDao' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed;
nested exception is java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-lazy-init="true">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/Java2all"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Student.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="studentDao" class="com.dao.impl.StudentDaoHibernate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="studentManager" class="com.manager.StudentManagerImpl">
<property name="studentDao" ref="studentDao" />
</bean>
</beans>
student.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name ="com.springhibnernate.Student" table="Student">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" length="40"/>
</property>
</class>
</hibernate-mapping>
There is a good chance you are missing "JTA-X.X.jar" in your classpath.
I've created custom spring scope using code found in this link which enables the use of view scoped Spring managed bean similar to that of JSF2 #ViewScoped.
When user presses the Submit button the following takes place:
- Backbean AA (#Scope(value="view") calls injected #Service BB
- #Service BB calls DAO class CC
- CC is #Transactional(readOnly = true) and has sessionFactory injected
private SessionFactory sessionFactory;
//inject sessionGactory
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
- in CC i set the #Transactional(readOnly = false, propagation = Propagation.REQUIRED)
for functions such as save/delete/saveOrUpdate
The Hibernate mapping concerning this class are:
RegUser.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.testApp.hibernate.RegUser" table="reg_users" schema="dbo" catalog="devDB">
<id name="userPk" type="java.lang.Long">
<column name="userPK" />
<generator class="identity" />
</id>
<property name="firstname" type="java.lang.String">
<column name="firstname" length="25" not-null="true" />
</property>
<property name="lastname" type="java.lang.String">
<column name="lastname" length="50" not-null="true" />
</property>
<set name="UserVsOrderses" inverse="true">
<key>
<column name="userPK" not-null="true" />
</key>
<one-to-many class="com.testApp.hibernate.UserVsOrders" />
</set>
</class>
</hibernate-mapping>
UserVsOrders.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.testApp.hibernate.custOrders" table="custOrders" schema="dbo" catalog="devDB">
<composite-id name="id" class="com.testApp.hibernate.custOrdersId">
<key-property name="userPk" type="java.lang.Long">
<column name="userPK" />
</key-property>
<key-property name="OrderPk" type="java.lang.Long">
<column name="OrderPK" />
</key-property>
</composite-id>
<many-to-one name="RegUser" class="com.testApp.hibernate.RegUser" update="false" insert="false" fetch="select">
<column name="userPK" not-null="true" />
</many-to-one>
<many-to-one name="Order" class="com.testApp.hibernate.Order" update="false" insert="false" fetch="select">
<column name="OrderPK" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.testApp" />
<context:annotation-config />
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="com.testApp.util.ViewScope" />
</entry>
</map>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/devDB">
</property>
<property name="username" value="blabla"></property>
<property name="password" value="blabla"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="c3p0.acquire_increment">1</prop>
<props>
</property>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
...
<!-- some other beans declared below -->
...
<!-- all the Mapping Resource -->
</beans>
My Problem is that one first form submit all is well, but on the next form submit i get:
failed to lazily initialize a collection, no session or session was closed
viewId=/page/regNewCust.xhtml
location=C:\repo\dev\WebRoot\nz\regNewCust.xhtml
phaseId=PROCESS_VALIDATIONS(3)
Caused by:
org.hibernate.LazyInitializationException - failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:394)
The page works fine if i set bean scope as #Scope(value='request')
I have tried adding the folloiwng to web.xml but it didn't help:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I've also tried adding <set name="UserVsOrderses" inverse="true" fetch="join"> which didn't help either.
Can anyone point of what i'm doing wrong and how i can fix the issue?
Found the solution here:
Stackoverflow link
I needed to add
<f:attribute name="collectionType" value="java.util.HashSet" />
to all the <h:selectManyListbox>
I'm trying to create a JSF2 application which uses Spring3 for the DI and hibernate to handle the persistence.
I'm stock on the following error and don't know how to proceed:
26/10/2012 1:57:04 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Unable to read XML
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:685)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:272)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 36 more
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 43 more
my applicationContext.xml is listed below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.myapp.techrro" />
<context:annotation-config />
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="com.myapp.util.ViewScope" />
</entry>
</map>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/myapp">
</property>
<property name="username" value="mydb"></property>
<property name="password" value="somepass"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="c3p0.acquire_increment">1</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/myapp/techrro/hibernate/UserOrderHist.hbm.xml
</value>
<value>com/myapp/techrro/hibernate/UserAccessHist.hbm.xml
</value>
<value>com/myapp/techrro/hibernate/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userOrderHistDAO" class="com.myapp.techrro.hibernate.userOrderHistDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userAccessHistDAO" class="com.myapp.techrro.hibernate.userAccessHistDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userDAO" class="com.myapp.techrro.hibernate.userDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
Update:
below are my mapping files, they were generated by myEclipse:
User.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.myapp.techrro.hibernate.User" table="User" schema="dbo" catalog="mydb">
<id name="user_PK" type="java.lang.Long">
<column name="user_PK" />
<generator class="identity" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" not-null="true" unique="true" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="40" not-null="true" />
</property>
<property name="userRole" type="java.lang.String">
<column name="userRole" length="1" not-null="true" />
</property>
<property name="active" type="java.lang.String">
<column name="active" length="1" />
</property>
<property name="type" type="java.lang.String">
<column name="type" length="3" not-null="true" />
</property>
<property name="firstName" type="java.lang.String">
<column name="firstName" length="30" />
</property>
<property name="lastName" type="java.lang.String">
<column name="lastName" length="30" />
</property>
<set name="userOrderHistLog" inverse="true">
<key>
<column name="user_PK" />
</key>
<one-to-many class="com.myapp.techrro.hibernate.userOrderHist" />
</set>
<set name="userAccessHistLog" inverse="true">
<key>
<column name="user_PK" not-null="true" />
</key>
<one-to-many class="com.myapp.techrro.hibernate.userAccessHist" />
</set>
</class>
</hibernate-mapping>
UserOrderHist.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.myapp.techrro.hibernate.userOrderHist" table="userOrderHist" schema="dbo" catalog="mydb">
<id name="userOrderHis_PK" type="java.lang.Long">
<column name="userOrderHis_PK" />
<generator class="identity" />
</id>
<many-to-one name="user" class="com.myapp.techrro.hibernate.User" fetch="select">
<column name="user_PK" />
</many-to-one>
<property name="action" type="java.lang.String">
<column name="action" length="1" not-null="true" />
</property>
<property name="prod" type="java.lang.String">
<column name="username" length="20" not-null="true" />
</property>
<property name="type" type="java.lang.String">
<column name="userRole" length="1" />
</property>
<property name="Updated" type="java.lang.String">
<column name="deleteFG" length="1" />
</property>
</class>
</hibernate-mapping>
UserAccessHist.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.myapp.techrro.hibernate.userAccessHist" table="userAccessHist" schema="dbo" catalog="mydb">
<id name="userAccessHistPK" type="java.lang.Long">
<column name="userAccessHistPK" />
<generator class="identity" />
</id>
<many-to-one name="user_PK" class="com.myapp.techrro.hibernate.User" fetch="select">
<column name="adminUserPK" not-null="true" />
</many-to-one>
<property name="createdDate" type="java.sql.Timestamp">
<column name="createdDate" length="23" not-null="true" />
</property>
<property name="accessDetail" type="java.lang.String">
<column name="accessDetail" length="100" not-null="true" />
</property>
</class>
</hibernate-mapping>
Can someone please possible causes of such error? Thanks
Based on your stacktrace (org.hibernate.InvalidMappingException: Unable to read XML), the problem is most likely in one of your hibernate mapping XML files.
Update:
Now that you have posted your hibernate files, it appears that you have an extra space at the end of the DTDs. Removing the trailing spaces should fix the problem.
I'm learning spring hibernate zk stack, and doing my first crud following this tutorial
I put applicationContext.xml into webapp/WEB-INF, and .hbm.xml to resources/mappings
But I dont know why my hbm files keep showing can not find my pojos.
in github https://github.com/kossel/firstzk
I have this structure
applicationContext.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- set other Hibernate properties in hibernate.cfg.xml file -->
<property name="configLocation" value="classpath:/com/iknition/firstzk/hibernate/hibernate.cfg.xml" />
</bean>
hibernate.cfg.xml
<mapping resource="com/iknition/firstzk/hibernate/Company.hbm.xml" />
<mapping resource="com/iknition/firstzk/hibernate/Contact.hbm.xml" />
Company.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iknition.firstzk.beans">
<class name="Contact" table="contact">
<id name="idcontact" column="idcontact" type="integer">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" />
<property name="email" column="email" type="string" />
<many-to-one name="company" column="companyId" class="com.iknition.firstzk.beans.Company" outer-join="true" />
</class>
</hibernate-mapping>
Contact.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iknition.firstzk.beans">
<class name="Contact" table="contact">
<id name="idcontact" column="idcontact" type="integer">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" />
<property name="email" column="email" type="string" />
<many-to-one name="company" column="companyId" class="com.iknition.firstzk.beans.Company" outer-join="true" />
</class>
</hibernate-mapping>
UPDATE:
I have reference to contact.hbm.xml too, I missed to put it here.
by "why my hbm files keep showing can not find my pojos" I mean, when I try to build the application, I keep getting error of "Caused by: org.hibernate.MappingException: entity class not found: com.iknition.firstzk.beans.Contact" I have changed many times the location of those configuration files, and still getting same error.
Hmmm... never tried using an external hibernate.cfg.xml. But I think specifying that only loads the properties. You might still need to set the mapping in a separate property.
Here's what my config usually looks like:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<bean
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="propertiesArray">
<list>
<props>...</props>
</list>
</property>
</bean>
</property>
<property name="mappingLocations" value="classpath:com/iknition/firstzk/hibernate/*.hbm.xml"/>
</bean>
I think you have to specify mappingLocations one-by-one like:
<property name="mappingLocations">
<util:list>
<value>hibernate/Company.hbm.xml</value>
<value>hibernate/Contact.hbm.xml</value>
</util:list>
</property>