Results 1 to 3 of 3

Thread: Getting Hibernate Error

  1. #1
    Join Date
    Feb 2012
    Posts
    20

    Default Getting Hibernate Error

    I am working on my first hibernate project. I have been trying to get this to work for so long that my head is throbbing. Can someone spot the error that is giving me the "No CurrentSessionContext configured!" error? I am sure I have missed something simple.


    Code:
    @Repository
    public class ContentComponentDAOImpl implements ContentComponentDAO {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        public ContentComponentDAOImpl(SessionFactory sessionFactory) {
            setSessionFactory(sessionFactory);
        }
    
        @Override
        public List<ContentComponent> getContentComponents() throws DAOException {
            Session session = sessionFactory.getCurrentSession();
            List<ContentComponent> documents = null;
            try {
                documents = (List<ContentComponent>) session.createQuery("from cmsutil.cmsutil.wp_content_components").list();
            } catch (HibernateException e) {
                e.printStackTrace();
            }
            return documents;
        }

    Code:
    public class Driver {
        public static void main(String[] args) throws DAOException {
    
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();
            ContentComponentDAOImpl c = new ContentComponentDAOImpl(sessionFactory);
            List l = c.getContentComponents();
        }


    Code:
    <?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>
            <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://mydrupal:3306/cmsutil</property>
            <property name="hibernate.connection.username">cms</property>
            <property name="hibernate.connection.password">xxxx</property>
        </session-factory>
    </hibernate-configuration>
    applicationcontext.xml
    Code:
        <bean id="dataSource">
            class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            p:driverClassName="${jdbc.driverClassName}"
            p:url="${jdbc.url}"
            p:username="${jdbc.username}"
            p:password="${jdbc.password}" 
        </bean>
    </beans>

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

    Default

    You aren't using spring... You have a nice xml file but that does nothing at all...

    I suggest a read of the spring reference guide about the hibernate integration part.

    1) Configure the Sessionfactory in your application context
    2) Don't use that contraption you have to load/configure hibernate (use the instance configurd in 1)
    3) Configure transactions (see tx chapters in reference guide)
    4) load xml file using an ApplicationContext (ClassPathXmlApplicationContext will do just fine).
    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

    Default

    Marten is correct,

    I do not see any spring configuration and only see hibernate configurations. Also you havnt configured sessionFactory using LocalSessionFactory bean. It will be nice to go through "Hitting Database" chapter of Spring in Action to have a clear understanding how to configure Spring and Hibernate. Hope this works. Let me know if you still have problem. We will solve it.

    Rohan Walia
    www.springaddon.com

Posting Permissions

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