Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Sunday, December 28, 2014

Displaying the Greeting Message based on Time in MySQL


Hi, My requirement is based on Current time i want to display Greeting message.

For example, if time is less than 12PM then i need to display "Morning" and if time is less than 5PM then i need to display "Afternoon" and if time is above 5 PM i need to display "Evening".

Step1: Selecting the Current time.
SELECT now() from DUAL;
Output:
 '2014-12-28 17:35:46'

Step2: In the Current time getting the hours.
SELECT TIME_FORMAT(now(),'%H') from DUAL;
Output: 
17

Step3: Now based on hours we need to display greeting message. So we need to use if condition.
SELECT IF((SELECT TIME_FORMAT(now(),'%H') from DUAL) < 12,'Morning', IF((SELECT TIME_FORMAT(now(), 
'%H') from DUAL) < 17,'Afternoon','Evening'));
Output:
'Evening'

Happy coding!

Saturday, December 27, 2014

How to get all table names using Hibernate.

This post demonstrates the how to get the all table names using Hibernate.
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();

// Way1 - get the all Class Mapping Information using Configuration
Iterator<PersistentClass> classMappings = configuration.getClassMappings();
while(classMappings.hasNext()) {
PersistentClass persistentClass = classMappings.next();
Table table = persistentClass.getTable();
String tableName = table.getName();
System.out.println(tableName);
}

// Way2 - get the all Class Metadata Information using SessionFactory
Map<String, ClassMetadata> classMetaDataMap = sessionFactory.getAllClassMetadata();
for(Map.Entry<String, ClassMetadata> metaDataMap : classMetaDataMap.entrySet()) {
ClassMetadata classMetadata = metaDataMap.getValue();
AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) classMetadata;
String tableName = abstractEntityPersister.getTableName();
System.out.println(tableName);
}

// Way3 - using Session
SessionImpl sessionImpl = (SessionImpl) sessionFactory.openSession();
try {
Connection connection = sessionImpl.connection();
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[] {"TABLE"});
while(resultSet.next()) {
String tableName = resultSet.getString(3);
System.out.println(tableName);
}
} catch (Exception e) {
e.printStackTrace();
}
References:
  • https://stackoverflow.com/questions/4813122/get-all-table-names-set-up-in-sessionfactory
  • http://www.herongyang.com/JDBC/sqljdbc-jar-Table-List.html
  • http://tutorials.jenkov.com/jdbc/databasemetadata.html

Tuesday, June 3, 2014

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at com.ranga.MultiTenancyDemo.main(MultiTenancyDemo.java:34)
 

Problem:

// Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration();
       
         System.out.println("Hibernate Configuration loaded.");
       
        //apply configuration property settings to StandardServiceRegistryBuilder
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
        System.out.println("Hibernate serviceRegistry created.");
       
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        System.out.println("Hibernate sessionFactory object created.");    

Solution:

After creation of Configuration object, we need to call the         configuration.configure("hibernate.cfg.xml"); method.

Thursday, April 3, 2014

Exception in thread "main" org.hibernate.HibernateException: persist is not valid without active transaction



Problem:  The problem in Configuration file.

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">       
        <property name="dataSource" ref="dataSource"></property>       
        <!-- Configuring the  Hibernate properties -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>               
                <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
            </props>       
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.ranga.Employee</value>
            </list>
        </property>       
    </bean>

------------------------------------------------------------------------------------------------------------------------------

hibernate.current_session_context_class is required only for JTA transactions.

Solution: Remove the hibernate.current_session_context_class property in sessionFactory. Now the code looks like

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">       
        <property name="dataSource" ref="dataSource"></property>       
        <!-- Configuring the  Hibernate properties -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>               
            </props>       
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.ranga.Employee</value>
            </list>
        </property>       
    </bean>

Click here to see more information.



java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

Problem:

While doing transactions management with the annotations, prior to Spring 3.0 we are adding aopalliance.jar file. In latest Spring bundle this jar file not available. If we add below line applicationContext.xml we need to add the aopalliance.jar file other wise it will throws the java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

Solution: Add the aopalliance.jar file to classpath or pom.xml file.

Click here to Download