Hibernate Architecture: Complete Overview
The project follows a standard Maven directory structure with a separation of concerns:
The pom.xml file:
The hibernate.cfg.xml file in the resources folder is crucial for configuring Hibernate:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/adv_java?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true</property>
<property name="hibernate.connection.username">root</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Session management -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Connection pooling -->
<property name="hibernate.connection.pool_size">10</property>
<!-- Echo SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Database schema auto-update -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Entity mapping -->
<mapping class="com.sunbeam.entities.User"/>
</session-factory>
</hibernate-configuration>
Key annotations:
hibernate.connection.*
: Database connection settingshibernate.dialect
: Tells Hibernate how to generate SQL for a specific databasehibernate.current_session_context_class
: "thread" means sessions are scoped to the current threadhibernate.connection.pool_size
: Sets the connection pool size to 10