I tried to test whether the hibernate configuration is working properly. I tried but I got an error:
INFO: HHH000206: hibernate.properties not found
To do this: I create:
[1] hibernate configuration file [using xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">explorecalifornia</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.connection.password">abc123</property>
</session-factory>
</hibernate-configuration>
[2] A hibernate utility class
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static{
try{
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch(HibernateException exception){
System.out.println("Problem creating session factory");
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtilities.sessionFactory = sessionFactory;
}
}
[3] The main program:
import org.hibernate.Session;
public class Program {
public static void main(String[] args) {
System.out.println("Hibernate");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.close();
}
}
But i got the following thing when I run the program:
Hibernate
Sep 29, 2013 10:47:15 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Sep 29, 2013 10:47:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 29, 2013 10:47:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Problem creating session factory
Exception in thread "main" java.lang.NullPointerException
at com.simpleprogrammer.Program.main(Program.java:10)
To solve this I tried Google and apply the ways which i found. but still i could not solve the problem. Can anybody please help me?
Place a file named hibernate. properties in a root directory of the classpath. Set System properties using java -Dproperty=value .
Click File > New . In the New wizard, locate Hibernate and then click Hibernate XML Mapping file (hbm. xml) .
That's only an INFO message telling that you don't have a hibernate.properties
file. This property file is not mandatory and so it's not what prevents your application from working.
If you want to know what caused the SessionFactory
creation failure, you need to change your catch block to:
catch(HibernateException exception){
System.out.println("Problem creating session factory");
exception.printStackTrace();
}
You should use a logging framework instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With