I do not want to have the hibernate.cfg.xml file. Rather I want to do all the configuration through my code as shown below.
private static final SessionFactory factory;
private static final Properties properties;
static
{
properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/books");
properties.setProperty("hibernate.connection.username", "jhtp7");
properties.setProperty("hibernate.connection.password", "password");
properties.setProperty("hibernate.show_sql", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
factory = new Configuration().setProperties(properties).configure().
buildSessionFactory();
}
I have tried the above approach. But I am facing the problem where hibernate throws an exception saying "./hibernate.cfg.xml file missing
".
Is it really mandatory to keep the hibernate.cfg.xml
file?
Thanks in advance
I believe it is because of your configure()
call on the Configuration
object. Try removing that and hibernate will not look for the non-existent file. Basically you are setting all the required properties via your properties
object so there is no real need to tell Hibernate to look for a hibernate.cfg.xml
file which is exactly what the configure()
method does.
No, I don't think the config xml is mandatory. To fix your problem, I think you need to use the org.hibernate.cfg.Configuration class. Have a look at this link: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html
Basically, you need to things like
Configuration cfg = new Configuration() .setProperty("hibernate.dialect", "com.mysql.jdbc.Driver") .setProperty("hibernate.connection.datasource", "jdbc:mysql://localhost:3306/books") .setProperty("hibernate.order_updates", "true");
Then to create your sessionFactory, you just say, cfg.buildSessionFactory();
No, it's not mandatory to use hibernate.cfg.xml. Just don't use .configure()
.
Hibernate will look for hibernate.cfg.xml if we use .configure()
.
public class HibernateUtil {
private static SessionFactory sessionFactory ;
static {
Configuration configuration = new Configuration();
configuration.addAnnotatedClass (org.gradle.Person.class);
configuration.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "root");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty("hibernate.hbm2ddl.auto", "update");
configuration.setProperty("hibernate.show_sql", "true");
configuration.setProperty(" hibernate.connection.pool_size", "10");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
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