Saturday 31 December 2022

Hibernate 6: Get hibernate dialect

Approach 1: Using SessionFactory.

Dialect dialect1 = ((SessionFactoryImplementor) SESSION_FACTORY).getJdbcServices().getDialect();

 

Approach 2: Using Hibernate session.

Dialect dialect2 = ((SessionFactoryImplementor) session.getSessionFactory()).getJdbcServices().getDialect();

Find the below working application.

 

Step 1: Create new maven project ‘hibernate-get-dialect’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>hibernate-get-dialect</artifactId>
    <version>1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>15</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.1.2.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>


    </dependencies>
</project>

Step 3: Create hibernate.cfg.xml file under src/main/resources folder.

 

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>

        <!-- Database Connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://127.0.0.1:5432/test</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>

        <!-- Enable the logging of all the generated SQL statements to the console -->
        <property name="show_sql">true</property>

        <!-- Format the generated SQL statement to make it more readable, -->
        <property name="format_sql">true</property>

        <!-- Hibernate will put comments inside all generated SQL statements to 
            hint what’s the generated SQL trying to do -->
        <property name="use_sql_comments">false</property>

        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <property name="hbm2ddl.auto">update</property>


    </session-factory>

</hibernate-configuration>

Step 4: Define main application class.

 

App.java

package com.sample.app;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.NationalizationSupport;
import org.hibernate.engine.spi.SessionFactoryImplementor;

public class App {
    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {

            final StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
                    .configure("hibernate.cfg.xml").build();

            final Metadata metaData = new MetadataSources(standardRegistry).getMetadataBuilder().build();

            return metaData.getSessionFactoryBuilder().build();

        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }

    }
    
    private static void printNationalizationSupportInfo(Dialect dialect) {
        NationalizationSupport nationalizationSupport = dialect.getNationalizationSupport();
        
        System.out.println("\n\nnationalizationSupport.getCharVariantCode() : " + nationalizationSupport.getCharVariantCode());
        System.out.println("nationalizationSupport.getClobVariantCode() : " + nationalizationSupport.getClobVariantCode());
        System.out.println("nationalizationSupport.getLongVarcharVariantCode() : " + nationalizationSupport.getLongVarcharVariantCode());
        System.out.println("nationalizationSupport.getVarcharVariantCode() : " + nationalizationSupport.getVarcharVariantCode());

    }

    public static void main(final String args[]) {
        Dialect dialect1 = ((SessionFactoryImplementor) SESSION_FACTORY).getJdbcServices().getDialect();
        printNationalizationSupportInfo(dialect1);
        
        try(Session session = SESSION_FACTORY.openSession()){
            Dialect dialect2 = ((SessionFactoryImplementor) session.getSessionFactory()).getJdbcServices().getDialect();
            printNationalizationSupportInfo(dialect2);
        }
        

    }
}

Total project structure looks like below.




Run App.java, you will see below messages in the console.

nationalizationSupport.getCharVariantCode() : 1
nationalizationSupport.getClobVariantCode() : 2005
nationalizationSupport.getLongVarcharVariantCode() : -1
nationalizationSupport.getVarcharVariantCode() : 12


nationalizationSupport.getCharVariantCode() : 1
nationalizationSupport.getClobVariantCode() : 2005
nationalizationSupport.getLongVarcharVariantCode() : -1
nationalizationSupport.getVarcharVariantCode() : 12

You can download complete working application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment