Prompt Title: Creating CRUD Application in Java.

Created 1 year ago
Votes 0
Views 383 times
0

Prompt Detail:

Sure, here's an example of a Hibernate configuration class:

java
import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateConfig { private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { try { Configuration configuration = new Configuration(); // Configure Hibernate properties configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:test"); configuration.setProperty("hibernate.connection.username", "sa"); configuration.setProperty("hibernate.connection.password", ""); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.hbm2ddl.auto", "create"); // Add entity classes configuration.addAnnotatedClass(Employee.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()) .build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { e.printStackTrace(); } } return sessionFactory; } }

This configuration class uses the H2 database and configures Hibernate properties such as the JDBC URL, dialect, and hbm2ddl.auto mode. You can modify these properties based on your specific requirements.

Additionally, the addAnnotatedClass() method is used to specify the entity classes that should be managed by Hibernate. You can add more entity classes to this method as needed.

The getSessionFactory() method creates a SessionFactory object using the configuration properties and the entity classes. The SessionFactory object is a factory for Session instances, which are used to perform database operations. The getSessionFactory() method is a static method, so it can be accessed from anywhere in your application.

avatar
Alexey Fomichev
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0
OIDC token example
1 year ago 2023-03-21 07:53:49 Kim Donghyeon
0
0
Java Web Programming Basics.
1 year ago 2023-03-30 11:25:56 ashwani
0
0
Return byte from url
1 year ago 2023-03-30 11:38:15 MoiseGui
0
0
0
0
0
0
Lab sample management system.
1 year ago 2023-04-08 17:02:15 Boniface Chacha
0
0
Redeclaring Variables in Java.
1 year ago 2023-04-14 05:54:49 Rohit
0
0
Java work report.
1 year ago 2023-04-15 07:36:32 yincx
0
0
Adding Date Picker.
1 year ago 2023-04-19 12:55:44 Mayur
0
0
Data Exchange (UI to Controller)
1 year ago 2023-04-19 18:17:57 Teja
0
0
Ebook html
1 year ago 2023-04-21 05:07:11 darrat
0
0
Java Developer Summary.
1 year ago 2023-04-21 17:06:40 Himanashu
0
0
Hibernate Projections и Entity Graph
1 year ago 2023-05-07 17:15:07 alex
0
0
Matematika di Java
1 year ago 2023-05-16 02:38:06 mathjava2soal
0
0
Java Class Creation Methods
1 year ago 2023-07-02 08:19:52 Javokhir
0
0
Java syntax
1 year ago 2023-07-04 13:34:54 Dahbi Ayoub
0
0
Java Operators Overview.
1 year ago 2023-08-03 04:48:22 Bhanu Prakash
0
34
CountDownLatch
1 year ago 2023-09-20 07:49:16 qwerty