2

I'm doing small project using hibernate and MySQL.im getting this error

org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:409)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:119)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
    at com.sla.tecprof.daoImpl.LoginDAOImpl.getUser(LoginDAOImpl.java:22)
    at com.sla.tecprof.bdImpl.LoginBDImpl.getUser(LoginBDImpl.java:28)
    at com.sla.tecprof.bdImpl.LoginBDImpl$$FastClassByCGLIB$$798cbe95.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:111)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:124)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    at com.sla.tecprof.bdImpl.LoginBDImpl$$EnhancerByCGLIB$$9f1f562a.getUser(<generated>)
    at com.sla.tecprof.action.LoginAction.validateUser(LoginAction.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
    at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:113)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

` my code is I think error is in buildSessonfactory.Is there any short way to check DB connection is success or not

List<Login> list = new ArrayList<Login>();
        SessionFactory sessionFactory = new Configuration().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("From Login lg where lg.userName = :userName and  lg.password = password ");
        query.setParameter("userName", login.getUserName());
        query.setParameter("password", login.getPassword());
        list = query.list();

please help

My configuration file is :-

<beans>
<bean id="hibernateDataSourse" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/tdp_management_system" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="hibernateDataSourse" />
    <property name="annotatedClasses">
        <list>
            <value>com.sla.tecprof.domain.Login</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
5
  • I guess you are using Spring, why this line is there? SessionFactory sessionFactory = new Configuration().buildSessionFactory(); You have already created one sessionFactory by the name of mySessionFactory try using it Commented Oct 9, 2015 at 3:52
  • Yes im using Spring sir. im new hibernate.I just use it for create sessionFactory object Commented Oct 9, 2015 at 3:56
  • Please check if your hibernate.cfg.xml file is in your classpath. Otherwise SessionFactory sessionFactory = new Configuration().buildSessionFactory(); cannot find the configuration file by default. Or you could specify the hibernate.cfg.xml location Commented Oct 9, 2015 at 3:58
  • Try injecting the session factory instance of "mySessionFactory" in your class for login. and then use that instance in place of building a new session factory Commented Oct 9, 2015 at 4:00
  • Thanx Bilbo Baggins.You Show me the path Commented Oct 9, 2015 at 7:42

3 Answers 3

1

You have not used the SessionFactory Bean defined in your spring xml file, you have used your default configuration file instead of the spring bean as your SessionFactory. Check your hibernate.cfg.xml file, you have not added the property hibernate.dialect there.

Only thing I have to say is you are out of spring-mvc structure. So, follow the mvc structure with and use the session factory defined as a spring beans. Use spring dependency injection to set the SessionFactory in your java class as follows.

@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sessionFactory;

If you are not need to follow spring-mvc, remove SessionFactory bean form your spring xml file and the DataStore also. Then define all settings with hibernate.cfg.xml configuration file. But remember, don't build the SessionFactory every time when using. Use static or ThreadLogical Session factory for your needs.

Sign up to request clarification or add additional context in comments.

Comments

1

This kind of exception occurs mostly if your MySQL/Oracle Service is not started or connection to particular database machine is log off on network or locally does'not allow to take the connection while you specified the Hibernate dialect correct.So ensure the above things is proper or not.

Comments

0

If you are instantiating your SessionFactory through the traditional / utility class but not by Spring, you might consider using the following line:

SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

I second @Bilbo Baggins, when you've already configured sessionFactory in your Spring configuration, you could very well use mysessionFactory bean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.