How to use hibernate.properties file instead of hibernate.cfg.xml
Asked Answered
M

7

18

I am trying to connect to DB in a servlet using Hibernate.I have read that we can use either hibernate.cfg.xml or hibernate.properties file for configuration of session.For me it worked with xml. Now when I am trying to use properties instead of xml its not working. It is saying that hibernate.cfg.xml not found.But nowhere I mentioned to use xml file and infact I have deleted that xml file.

Please Help me. And Please correct me if I am doing anything wrong.

Margo answered 28/5, 2014 at 10:5 Comment(1)
Please share your hibernate.properties file.Scarito
P
11

From what i understood from hibernate the best thing to do is to define the mapping in the hibernate.cfg.xml file and other configurations in the hibernate.properties.

An alternative approach to configuration is to specify a full configuration in a file named hibernate.cfg.xml. This file can be used as a replacement for the hibernate.properties file or, if both are present, to override properties.

The hibernate.cfg.xml is also more convenient once you have to tune the Hibernate cache. It is your choice to use either hibernate.properties or hibernate.cfg.xml. Both are equivalent.

You can read more about this in the following link:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

Pinxit answered 28/5, 2014 at 10:14 Comment(1)
how to replace <mapping resource=""> in properties file?.Washedout
D
20

This code will call hibernate.cfg.xml by default:

Configuration configuration = new Configuration().configure();

And this code will call hibernate.properties by default:

Configuration configuration = new Configuration();

Hope it helps.

Diathesis answered 22/6, 2016 at 18:0 Comment(1)
What I needed! Thanks.Chemnitz
P
11

From what i understood from hibernate the best thing to do is to define the mapping in the hibernate.cfg.xml file and other configurations in the hibernate.properties.

An alternative approach to configuration is to specify a full configuration in a file named hibernate.cfg.xml. This file can be used as a replacement for the hibernate.properties file or, if both are present, to override properties.

The hibernate.cfg.xml is also more convenient once you have to tune the Hibernate cache. It is your choice to use either hibernate.properties or hibernate.cfg.xml. Both are equivalent.

You can read more about this in the following link:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

Pinxit answered 28/5, 2014 at 10:14 Comment(1)
how to replace <mapping resource=""> in properties file?.Washedout
S
5

Remove .configure() if you are using hibernate.properties. Below code is HibernateUtil session factory implementation.

private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        ServiceRegistry serviceRegistry
            = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        configuration.addAnnotatedClass(LookUpModel.class);
        return configuration
                .buildSessionFactory(serviceRegistry);
    }catch(Exception e) {
        e.printStackTrace();
        throw new RuntimeException("There is issue in hibernate util");
    }
}

hibernate.properites file

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password = passsword
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Sociopath answered 16/7, 2018 at 11:34 Comment(0)
P
1

If you are using a database from a servlet then you should define a DataSource in your server and point one hibernate property at that instead of defining everything via all the other hibernate properties you're probably using now.

This has the benefit of permitting you to define connection pooling and other connection related parameters independently of your application.

For example, your production environment is likely to have a different database password than your test and development environments.

Parakeet answered 28/5, 2014 at 10:15 Comment(0)
C
0
public class FactoryConfiguration {

    private static FactoryConfiguration factoryConfiguration;
    private final SessionFactory sessionFactory;

    private FactoryConfiguration(){
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("hibernate.properties"));
        }catch (IOException e) {
            e.printStackTrace();
        }
        Configuration configuration = new Configuration()
        .addProperties(properties)
                .addAnnotatedClass(Student.class);
        sessionFactory = configuration.buildSessionFactory();
    }

    public static FactoryConfiguration getInstance(){
        return (factoryConfiguration == null) ? factoryConfiguration = new FactoryConfiguration() : factoryConfiguration;
    }

    public Session getSession(){
        return sessionFactory.openSession();
    }
}
Ctn answered 25/12, 2021 at 6:0 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Bumpkin
B
0

You can add annonated entity class as below. This works for me.

Properties properties = new Properties();
   try{
       properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("hibernate.properties"));
   }catch (Exception e){
       System.out.println("cannot load properties file.");
   }
    sessionFactory = new Configuration().mergeProperties(properties).addAnnotatedClass(Customer.class).buildSessionFactory();

"hibernate.propeties" is the file that have hibernate configuration and please remember to give your hibernate property file location correctly.

Basketry answered 30/12, 2021 at 5:55 Comment(0)
E
0

In addition to above said.
Via StandardServiceRegistry. To load a random properties in property format as resource:

    String resourceName = "MyHibernate.properties";
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .loadProperties(resourceName).build();
    sessionFactory = new MetadataSources(registry)
            .addAnnotatedClass(SomeClass.class)
            .buildMetadata()
            .buildSessionFactory();

As xml resource:

    String resourceName = "MyHibernate.cfg.xml";
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure(resourceName).build();
    sessionFactory = new MetadataSources(registry)
            .addAnnotatedClass(SomeClass.class)
            .buildMetadata()
            .buildSessionFactory();

Via Configuration. Load as random file path,
as property file path

        String filePath = "filePath/MyHibernate.properties";
        Properties properties = new Properties();
        properties.load(new FileInputStream(filePath));
        SessionFactory sessionFactory = new Configuration()
                .addProperties(properties)
                .addAnnotatedClass(SomeClass.class)
                .buildSessionFactory();

as xml file path

        String filePath = "filePath/MyHibernate.cfg.xml";
        SessionFactory sessionFactory = new Configuration()
                .configure(new File(filePath));
                .addAnnotatedClass(SomeClass.class)
                .buildSessionFactory();

Load as resource,
as xml resource

    String resourceXmlName = "MyHibernate.cfg.xml";
    SessionFactory sessionFactory = new Configuration()
            .configure(resourceXmlName)
            .addAnnotatedClass(SomeClass.class)
            .buildSessionFactory();

To load properties as resource, load as standard path as described above, but transform resource name to file path like this:

    String resourceName = "MyHibernate.properties";
    URL fl = this.getClass().getClassLoader().getResource(resourceName);
    //fl can be null, up to you how to handle this
    System.out.println (fl.getPath())); 
Edora answered 27/11, 2023 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.