- Java Hibernate Cookbook
- Yogesh Prajapati Vishal Ranapariya
- 307字
- 2021-07-16 19:59:47
Configuring hibernate programmatically
In the preceding section, we understood XML and the properties-based configuration. Hibernate also supports the programmatic configuration. To configure hibernate using this method, we have to work on a Java code and create an instance of the org.hibernate.cfg.Configuration
class. There are multiple ways to configure hibernate.
How to do it…
First, write the following code:
Configuration configuration = new Configuration();
This will create an instance of the Configuration
class using hibernate.cfg.xml
or hibernate.properties
, whichever is found in the classpath.
Provide the following mapping files to the configuration:
configuration = configuration.addResource("Employee.hbm.xml"); configuration = configuration.addResource("Department.hbm.xml");
You can use an alternate way, as shown in the following code:
Configuration configuration = new Configuration().addResource("Employee.hbm.xml").addResource("Department.hbm.xml");
We can also provide a direct mapping using the class, as shown in the following code:
configuration = configuration.addClass("Department.class");
This will also look for Department.hbm.xml
.
We can also set a custom property. To set up the custom property, use the following method:
configuration.setProperty(propertyName, value);
For example, consider the following code:
configuration.setProperty("show_sql", true);
To set up multiple properties using the properties object, execute the following code:
configuration.setProperties(java.util.Properties properties);
Here is an example:
Properties properties = new Properties(); properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/kode12"); properties.put("hibernate.connection.username", "root"); properties.put("hibernate.connection.password", "root"); properties.put("show_sql", "true"); properties.put("hbm2ddl.auto", "update"); configuration.setProperties(properties);
To read the mapping from the URL, you can use the following code:
configuration = configuration.addURL(java.net.URL url);
To read the mapping from the XML file, you can use the following code:
configuration = configuration.addXML(String xml);
How it works…
When we select the programmatic configuration option, the Configuration
class is very important. Using the instance of the Configuration
class, we will build a SessionFactory
object, as shown in the following code:
SessionFactory sessionFactory = new Configuration().buildSessionFactory();
When the preceding code is executed, it creates a SessionFactory
object using a .properties
or .cfg
file or whichever source is provided to create the configuration.
- 軟件安全技術(shù)
- Access 2010數(shù)據(jù)庫基礎(chǔ)與應(yīng)用項目式教程(第3版)
- 老“碼”識途
- Web程序設(shè)計(第二版)
- Hands-On Reinforcement Learning with Python
- 用戶體驗增長:數(shù)字化·智能化·綠色化
- C語言程序設(shè)計
- 從0到1:HTML5 Canvas動畫開發(fā)
- 從程序員角度學(xué)習(xí)數(shù)據(jù)庫技術(shù)(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- PHP 8從入門到精通(視頻教學(xué)版)
- 青少年P(guān)ython趣味編程
- Spring Boot 3:入門與應(yīng)用實戰(zhàn)
- Django 3 Web Development Cookbook
- 大話C語言
- C++面向?qū)ο蟪绦蛟O(shè)計教程