How to use hibernate.properties file instead of hibernate.cfg.xml

后端 未结 4 1600
清酒与你
清酒与你 2021-02-13 00:36

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 i

4条回答
  •  無奈伤痛
    2021-02-13 00:59

    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
    

提交回复
热议问题