wood burning stoves 2.0*
The moose likes Spring and the fly likes Spring,JUnit, Hibernate and persistence db but not insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring,JUnit, Hibernate and persistence db but not insert" Watch "Spring,JUnit, Hibernate and persistence db but not insert" New topic
Author

Spring,JUnit, Hibernate and persistence db but not insert

Ricardo Llontop
Greenhorn

Joined: Apr 18, 2011
Posts: 11

This is my spring-master.xml

my spring-datasource.xml

and my spring-hibernate.xml

My Dao Class and Imp :



I want to probe this by Junit like this:

Run this by Junit test and take this:

The database is create but the data id-generated,ayambo,toledo not inserted, the database is Postgresql 9.0.3, please?, What i do wrong???
Pdta: I now that hibernatetemplate is unused, but the configuration is ok??(Sorry for my bad english.)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

You are using Spring in your unit test and transactions. The default behavior of transactions on test methods is to rollback any changes at the end of the test method. Based on your log at the end, it shows that Hibernate is running the insert and the select. But is your test failing?

Also, in your component-scan, you are excluding classes that are annotated with @Repository??? Or are they getting picked up by your second component-scan?

Also, be know, if you are using a version of Hibernate that is higher than 3.1, then you are gaining nothing by using the HibernateTemplate, and would be better of injecting the SessionFactory into your DAO and using straight Hibernate code.

So your code would then look like


You will still get HibernateExceptions translated to Spring's DataAccessException hierarchy with @Repository and your bean for the
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

Mark

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ricardo Llontop
Greenhorn

Joined: Apr 18, 2011
Posts: 11

Thanks Mark for reply, the only that I fail is that Junit roolback default true, I change this:
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false).

Thnks.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Ricardo Llontop wrote:Thanks Mark for reply, the only that I fail is that Junit roolback default true, I change this:
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false).

Thnks.



Yep, so now with that setting all the transactions will commit.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring,JUnit, Hibernate and persistence db but not insert
 
Similar Threads
@Transactional Annotation
HibernateDaoSupport and Transaction
XML name space error in Spring data JPA hibernate applicationContext.xml
How to define Hibernate SessionFactory with Spring applicationcontext.xml
org.springframework.beans.factory.BeanCreationException