Hibernate 4 Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

后端 未结 12 1726
青春惊慌失措
青春惊慌失措 2021-01-17 14:25

I am using Hibernate latest version 4.3.5.Final.

My hibernate.cfg.xml file content:



        
相关标签:
12条回答
  • 2021-01-17 14:49
    // Create Configuration File and configure it.
        Configuration configuration=new Configuration();
        configuration.configure("com/sathya/config/emp.cfg.xml"); 
        // Create Service Registry
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        // Create Session Object using SessionFactory Interface
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        Session session=sessionFactory.openSession();
        // new StandardServiceRegistryBuilder().applySettings(new Configuration().getProperties());
        // Session session=new Configuration().buildSessionFactory(new StandardServiceRegistryBuilder().build()).openSession();
    
    0 讨论(0)
  • 2021-01-17 14:52

    I encountered this issue when using the following:

    • Spring Framework / Data: 4.1.6;
    • Spring Boot: 1.2.3;
    • Hibernate provider for JPA: 4.3.8.

    I had SSL enabled on the database side as part of the host-based authentication mechanism (using Postgres) and the issue was that I hadn't specified the SSL JVM properties related to the key store containing the certificate of the client app when starting the application:

    -Djavax.net.ssl.keyStore=mykeystore.jks -Djavax.net.ssl.keyStorePassword=myPassword

    Note: I understand the answer has already been answered, but I'm leaving this answer for future reference.

    0 讨论(0)
  • 2021-01-17 14:53

    I once faced the exact same problem, and it turned out that the issue was that I had not started the database service locally (which I see you are also using a local database). That simple!!!

    0 讨论(0)
  • 2021-01-17 14:54

    Remove both lines of setting the dialect, Hibernate 4 does a fantastic job of determining which dialect to use based on the JDBC Driver you give it and the database that it connects too. You don't need to specify a dialect. If you force a dialect that is incorrect or doesn't quite match the driver (which can happen as dialects get updated more often than JDBC drivers do) Hibernate might not perform as expected.

    Configuring Hibernate using a hibernate.cfg.xml is how things were done when using Hibernate 3. Hibernate 4 supports XML configuration within the Spring Application context. (I'm assuming that you are using Spring) It hooks in really smoothly. I suggest that you switch to using the new method of configuring Hibernate as your configuration doctype is set for Hibernate 3.

    This is how you want to specify a dataSource for Hibernate 4 using the Spring application context.

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/Test" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>
    

    and then you wire it into your session factory like this:

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="package.where.your.annotated.objects.are" />
        <property name="hibernateProperties">
            <props>
                <!-- Uncomment this when testing -->
                <!--<prop key="hibernate.show_sql">true</prop>-->
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    0 讨论(0)
  • 2021-01-17 14:54

    It happened to me that I used the wrong username and password after I changed my connection string from remote address to localhost. Hope this will help.

    0 讨论(0)
  • 2021-01-17 15:02

    From the hibernate source code here, a StandardServiceRegistryBuilder instance is used to get different services from hibernate.cfg.xml, for example serviceRegistry.getService( JdbcServices.class ), serviceRegistry.getService( CacheImplementor.class ).

    The old configuration.buildSessionFactory() method is as below now:

    public SessionFactory buildSessionFactory() throws HibernateException {
        Environment.verifyProperties( properties );
        ConfigurationHelper.resolvePlaceHolders( properties );
        final ServiceRegistry serviceRegistry =  new StandardServiceRegistryBuilder()
                .applySettings( properties )
                .build();
        setSessionFactoryObserver(
                new SessionFactoryObserver() {
                    @Override
                    public void sessionFactoryCreated(SessionFactory factory) {
                    }
    
                    @Override
                    public void sessionFactoryClosed(SessionFactory factory) {
                        ( (StandardServiceRegistryImpl) serviceRegistry ).destroy();
                    }
                }
        );
        return buildSessionFactory( serviceRegistry );
    }
    
    0 讨论(0)
提交回复
热议问题