where to put applicationcontext.xml and .hbm files? - spring

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>

Related

Inserting autoincrement ID record into Java DB (Derby) using Hibernate session

I need to configure hibernate session manager for right inserting records with autoincrement ID. I use Java DB (Derby) and there's probably some problem with application of right dialect (hibernate uses last_insert_id() instead of IDENTITY_VAL_LOCAL()), but its definition looks ok. Or not? I do not have much experience with that yet, so thanks for a little hint.
Relevant part of servlet-context.xml:
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/sample"/>
<property name="username" value="app"/>
<property name="password" value="app"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="show_sql">true</property>
<mapping resource="net/codejava/spring/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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">
<hibernate-mapping package="net.codejava.spring.model">
<class name="User" table="USERS">
<id name="id" column="USER_ID">
<generator class="native"/>
</id>
<property name="username" column="USERNAME" />
<property name="password" column="PASSWORD" />
<property name="email" column="EMAIL" />
</class>
</hibernate-mapping>
Implementation code:
public void InsertUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
Server log:
Info: Hibernate: insert into USERS (USERNAME, PASSWORD, EMAIL) values (?, ?, ?)
Info: Hibernate: select last_insert_id()
WARN: SQL Error: 20000, SQLState: 42X01

How to configure Hibernate when you are using a Spring Session Factory

I am trying to setup Hibernate Tools in eclipse. The trouble is that it can't find any mapping files.
I have created a console configuration that points to my environment.properties file and hibernate.cfg.xml. The trouble is that there are no mappings in hibernate.cfg.xml.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
</session-factory>
</hibernate-configuration>
it seems that it is using the spring bean sessionFactory in myproject-persistence.xml (below) to find the required mapping files. I can't see anywhere that this file can be added to the the Console Configuration in eclipse.
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${hibernate.connection.driver_class}" />
<property name="jdbcUrl" value="${hibernate.connection.url}" />
<property name="user" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="25" />
<property name="acquireIncrement" value="5" />
<property name="maxIdleTime" value="1800" />
<property name="numHelperThreads" value="5" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
</props>
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="mappingLocations">
<list><value>classpath*:com/mybusiness/myproject/platform/api/**/*.hbm.xml</value></list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
</beans>
How can I get this working?
UPDATE
I managed to get a single Mapping working by adding it to the 'Mappings' tab in 'Edit Configuration'. However, I can't use wildcards here and would have to add every mapping manually.
Hibernate Tools is not available under Hibernate 4.x version. It is available with version 3.2. If you are using maven, the dependency goes as:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>3.2.4.GA</version>
<scope>runtime</scope>
</dependency>
Now, the hibernate configuration for the tools got nothing to do with spring. An example xml will be (values in this example is for sql server):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="reversengineeringfactory">
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://myinstance:port/mydb</property>
<property name="hibernate.connection.username">dbuser</property>
<property name="hibernate.connection.password">dbpass</property>
<property name="hibernate.default_catalog">mydb</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
</session-factory>
</hibernate-configuration>
Now, For configuring the hibernate config xml in eclipse, you should choose Hibernate Perspective --> Edit Configuration --> Go for configuration File Setup.
A sample reverse engineering xml is below (gives granular control over code generation)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-schema="dbo" match-name="Employee"
package="com.maggu.domain.model" />
<table-filter match-schema="dbo" match-name="Company"
package="com.maggu.domain.model" />
<table schema="dbo" name="Employee">
<primary-key>
<generator class="identity" />
</primary-key>
</table>
<table schema="dbo" name="Company">
<primary-key>
<generator class="identity" />
</primary-key>
</table>
</hibernate-reverse-engineering>
HTH
if you are using annotations to create your persistence entities than you can do like:
<bean id = "mySessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name = "packagesToScan">
<list>
<value>entityclasses</value>
</list>
</property>
where entityclasses is the package which contains all your entity classes. Let me know if it helps!!

JSF2 + Spring3 and hibernate: Spring Managed Bean (#Scope value="view") LazyInitializationException exception

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>

Integrating JSF2 with Spring3 and Hibernate4 - applicationContext.xml error?

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.

set transaction must be first statement of transaction With Spring.Data.NHibernate12

I am using Spring.Data.NHibernate12 on my database level.my application connection with database is not getting released.Sometime in Nhibernate log i am getting
set transaction must be first statement of transaction .Setting transaction tag on top of the function.
Underneath given is Dataconfiguration.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">
<object id="AuditLogger" type="Risco.Rsp.Ac.Audit.AuditLogger, Risco.Rsp.Ac.Audit" singleton="false">
<property name="CacheSettings" ref="CacheSettings"/>
</object>
<object id="CacheSettings"
type="Risco.Rsp.Ac.AMAC.CacheMgmt.Utilities.UpdateEntityCacheHelper, Risco.Rsp.Ac.AMAC.CacheMgmt.Utilities" singleton="false"/>
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="databaseSettings"/>
</object>
<db:provider id="AMACDbProvider"
provider="OracleClient-2.0"
connectionString="Data Source=RISCODEVDB;User ID=amsbvt; Password=amsuser1234;"/>
<!-- For creating Factory objects -->
<object id="NHibernateSessionFactory"
type="Spring.Data.NHibernate.LocalSessionFactoryObject,Spring.Data.NHibernate12">
<property name="DbProvider" ref="AMACDbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Risco.Rsp.Ac.AMAC.Mapping</value>
<value>Risco.Rsp.Ac.Logging.Appenders</value>
<value>Risco.Rsp.Ac.AMAC.CacheMappings</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect"
value="NHibernate.Dialect.Oracle9Dialect"/>
<entry key="hibernate.connection.driver_class"
value="NHibernate.Driver.OracleClientDriver"/>
</dictionary>
</property>
</object>
<object id="HibernateTemplate" type="Spring.Data.NHibernate.HibernateTemplate" singleton="false">
<property name="SessionFactory" ref="NHibernateSessionFactory" />
<property name="TemplateFlushMode" value="Auto" />
<property name="CacheQueries" value="true" />
<property name="EntityInterceptor" ref="AuditLogger"/>
</object>
<!-- To make use of spring's + nHibernate transaction management-->
<object id="transactionManager" singleton="true" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
<property name="DbProvider" ref="AMACDbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
<property name="EntityInterceptor" ref="AuditLogger"/>
</object>
<object id="CategoryDAOTx" type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject,Spring.Data">
<property name="PlatformTransactionManager" ref="transactionManager"/>
<property name="Target" ref="SendConfigDataDAO"/>
<property name="TransactionAttributes">
<name-values>
<add key="Save" value="PROPAGATION_REQUIRED"/>
<add key="Delete" value="PROPAGATION_REQUIRED"/>
<add key="SaveOrUpdate" value="PROPAGATION_REQUIRED"/>
<add key="FetchForUpdate" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>
<object id="SendConfigDataDAO" type="Risco.Rsp.Ac.AMAC.DAO.SendConfigDataMgmt.SendConfigDataDAO,Risco.Rsp.Ac.AMAC.DAO.SendConfigDataMgmt">
<property name="HibernateTemplate" ref="HibernateTemplate" />
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
<!--<tx:attribute-driven/>-->
</objects>
First of all, you need to update the NHibernate to 3 version.
Instead of using Spring.Data.NHibernate12, you must use Spring.Data.NHibernate30.
I am working with it very well.
Regards,

Resources