Announcement Announcement Module
Collapse
No announcement yet.
Hibernate NamedQuery Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate NamedQuery

    I am trying to wire-up my first Spring/Hibernate project, and I think I got everything cross-wired. The following code is giving me "Exception in thread "main" org.hibernate.HibernateException: getNamedQuery is not valid without active transaction" in the testing class at the bottom.

    Does anyone spot what I am doing wrong?

    DAO
    Code:
    import cms.entity.ContentComponent;
    import cms.exceptions.DAOException;
    import java.util.List;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    @Repository("contentComponentDAO")
    public class ContentComponentDAOImpl implements ContentComponentDAO {
    
        @Autowired
        private SessionFactory sessionFactory;
    
    
        public ContentComponentDAOImpl(SessionFactory sessionFactory) {
            setSessionFactory(sessionFactory);
        }
    
        
        public ContentComponentDAOImpl() {
        }
    
        @Override
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
    
        @Override
        public List<ContentComponent> getContentComponents() throws DAOException {
            Session session = sessionFactory.getCurrentSession();
            List<ContentComponent> documents;
    
            Query query = session.getNamedQuery("ContentComponent.findAllContentComponents");
            documents = query.list();
            return documents;
        }

    app config
    Code:
        <context:component-scan base-package="cms" />
        
        <tx:annotation-driven />      
    
        <bean id="transactionManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
    
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="hibernate.cfg.xml"/>
        </bean>
    
        <bean id="contentComponentDAO" class="cms.DAO.ContentComponentDAOImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>

    hibernate.cfg.xml
    Code:
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://drup:3306/cmsutil</property>
            <property name="hibernate.connection.username">cms</property>
            <property name="hibernate.connection.password">xxxx</property>
            <property name="hibernate.current_session_context_class">thread</property>
            <mapping resource="contentcomponent.hbm.xml"/>        
        </session-factory>
    </hibernate-configuration>

    testing class
    Code:
    import cms.dao.ContentComponentDAOImpl;
    import cms.exceptions.DAOException;
    import java.util.Iterator;
    import java.util.List;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class Driver {
    
        public static void main(String[] args) throws DAOException {
    
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    
            ContentComponentDAOImpl c = new ContentComponentDAOImpl(sessionFactory);
            c.setSessionFactory(sessionFactory);
            List l = c.getContentComponents();

  • #2
    Hello

    "Exception in thread "main" org.hibernate.HibernateException: getNamedQuery is not valid without active transaction
    Post the complete error stack trace.

    Is wise include the @Transactional for a repository to let Spring include its own control transactional control when a service class call a dao object. Therefore try

    Code:
    @Transactional
    @Repository("contentComponentDAO")
    public class ContentComponentDAOImpl implements ContentComponentDAO {
    Some observations

    1) Since you already use @Repository + context:component-scan, is really not necessary anymore

    Code:
      
    <bean id="contentComponentDAO" class="cms.DAO.ContentComponentDAOImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    2) The follow


    Code:
     
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    
            ContentComponentDAOImpl c = new ContentComponentDAOImpl(sessionFactory);
            c.setSessionFactory(sessionFactory);
            List l = c.getContentComponents();
    is wrong, because you are not using Spring!, where did you get the Spring's application context?

    You code is isolated from Spring

    Comment


    • #3
      Originally posted by dr_pompeii View Post
      Hello
      2) The follow


      Code:
       
              SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
      
              ContentComponentDAOImpl c = new ContentComponentDAOImpl(sessionFactory);
              c.setSessionFactory(sessionFactory);
              List l = c.getContentComponents();
      is wrong, because you are not using Spring!, where did you get the Spring's application context?

      You code is isolated from Spring
      Can you give me some more details on what I should do here? I know how to get an app context, but what do I do with it?

      Comment

      Working...
      X