org.hibernate.HibernateException:必须显式设置Hibernate Dialect

时间:2015-10-09 03:48:13

标签: java hibernate

我正在使用hibernate和MySQL.im进行小项目获取此错误

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)

` 我的代码是我认为错误是在buildSessonfactory.Is有任何简短的方法来检查数据库连接是否成功

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();

请帮助

我的配置文件是: -

<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>

3 个答案:

答案 0 :(得分:1)

您尚未使用spring xml文件中定义的SessionFactory Bean,您已使用默认配置文件而不是spring bean作为SessionFactory检查您的hibernate.cfg.xml文件,您尚未在其中添加属性hibernate.dialect

我唯一要说的是你没有spring-mvc结构。因此,请遵循mvc结构并使用定义为spring bean的会话工厂。使用spring依赖注入在java类中设置SessionFactory,如下所示。

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

如果您不需要关注spring-mvc,请从spring xml文件和SessionFactory中删除DataStore bean。然后使用hibernate.cfg.xml配置文件定义所有设置。但请记住,每次使用时都不要构建SessionFactory。根据您的需要使用staticThreadLogical会话工厂。

答案 1 :(得分:0)

如果您通过传统/实用程序类而不是Spring实例化SessionFactory,则可以考虑使用以下行:

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

我是第二个@Bilbo Baggins,当你已经在Spring配置中配置了sessionFactory时,你可以很好地使用 mysessionFactory bean

答案 2 :(得分:0)

这种异常主要发生在你的MySQL / Oracle服务没有启动或者特定数据库机器的连接在网络上注销或者本地没有允许在你指定Hibernate方言正确的情况下进行连接的情况下。确保上述事情是否正确。