Results 1 to 3 of 3

Thread: Hibernate JPA not resulting in insert query.

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Default Hibernate JPA not resulting in insert query.

    Hi,
    I'm trying to setup a project using hibernate jpa, spring, mysql and tomcat 6. I'm using hibernate jpa and a jpatransactionmanager. When I run a junit test on my dao I see the "Hibernate: insert into message (post_date, message_header, message_string) values (?, ?, ?)". But when I run the webapp hibernate does not report inserting the message. I've tried changing multiple things but nothing works. Does anyone see a mistake that I have made?

    I really don't understand it since i have the <tx:annotation-driven/> declaration I would assume Spring would find the @Transactional annotations. I have tried em.flush() after the persist but then I get an error that no transaction is currently ongoing. Can anyone please help me?

    I've done the following configuration:

    Code:
    <context:property-placeholder location="classpath*:META-INF/*.properties"/>
    
        <context:component-scan base-package="be.tim.fest"/>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${database.driverClassName}"/>
            <property name="url" value="${database.url}"/>
            <property name="username" value="${database.username}"/>
            <property name="password" value="${database.password}"/>
            <property name="initialSize" value="1"/>
            <property name="maxActive" value="5"/>
        </bean>
    
        <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
            <property name="packagesToScan" value="be.tim.fest"/>
    
        </bean>
    
        <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </bean>
    
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">    
           <property name="dataSource" ref="dataSource"/>
            <property name="entityManagerFactory" ref="emf"/>
        </bean>
    
        <tx:annotation-driven/>
    Code:
    @Service("messageService")
    @Transactional(readOnly = true, propagation = Propagation.REQUIRED)
    public class MessageServiceImp implements MessageService{
        
        @Inject
        private MessageDao messageDao;
        
        public List<Message> getAllMessages() {
            return messageDao.getAllMessages();
        }
    
        @Transactional(readOnly = false)
        public void addMessage(Message m) {
            messageDao.addMessage(m);
        }
    }
    Code:
    @Repository("messageDao")
    public class MessageDaoJpa implements MessageDao{
    
        private static final Logger logger = Logger.getLogger(MessageDaoJpa.class);
        
        @PersistenceContext
        private EntityManager em;
    
        public void addMessage(Message m) {
            em.persist(m);
            logger.info("Insert new message: " + m.toString());
            //em.flush();
        }
    
        public List<Message> getAllMessages() {
            return em.createQuery("select m from Message m", Message.class).getResultList();
        }
    }
    Thanks in advance!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Please use the search as this question has been answered numerous times before.

    My guess duplicate component-scan elements in both ContextLoaderListener en DispatcherServlet resulting in duplicate bean instances one correct one incorrect and the latter is used.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Default

    I have looked trough the forum (& the internet) but since the I thought the error came from hibernate/jpa I assumed it had something to do with how I declared the EntityManager or JPATransactionManager. Almost noone uses the same combination of spring/jpa/hibernate config.

    I checked and indeed I have a component scan in the dispatchterServlet, commenting this out seems to make the website not work anymore. I'll have to check the reference how to fix this.

    Update:
    I changed the <context:component-scan base-package="be.tim.fest.controller"/> to only look in the controller package and now i works like a charm.
    Last edited by lnulzer; Mar 13th, 2012 at 08:15 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •