Hibernate is a popular Object-Relational Mapping (ORM) framework for Java. Here’s a cheat sheet covering some common Hibernate concepts and operations:
Hibernate Configuration
Hibernate Configuration File (hibernate.cfg.xml
):
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database</property>
<property name="hibernate.connection.username">your_username</property>
<property name="hibernate.connection.password">your_password</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Entity Mapping
Entity Class:
@Entity
@Table(name = "your_table")
public class YourEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
// Other fields, getters, and setters
}
CRUD Operations
Save/Insert Entity:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
YourEntity entity = new YourEntity();
// Set entity properties
session.save(entity);
transaction.commit();
session.close();
Retrieve/Load Entity:
Session session = sessionFactory.openSession();
YourEntity entity = session.get(YourEntity.class, entityId);
session.close();
Update Entity:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
YourEntity entity = session.get(YourEntity.class, entityId);
// Modify entity properties
session.update(entity);
transaction.commit();
session.close();
Delete Entity:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
YourEntity entity = session.get(YourEntity.class, entityId);
session.delete(entity);
transaction.commit();
session.close();
HQL (Hibernate Query Language)
HQL Select Query:
String hql = "FROM YourEntity WHERE property = :value";
Query query = session.createQuery(hql);
query.setParameter("value", propertyValue);
List<YourEntity> resultList = query.getResultList();
Criteria API
Criteria Query:
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<YourEntity> criteriaQuery = builder.createQuery(YourEntity.class);
Root<YourEntity> root = criteriaQuery.from(YourEntity.class);
criteriaQuery.select(root).where(builder.equal(root.get("property"), propertyValue));
List<YourEntity> resultList = session.createQuery(criteriaQuery).getResultList();
Transactions
Programmatic Transaction:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// Hibernate operations
transaction.commit();
session.close();
Declarative Transaction (Spring):
@Transactional
public void someMethod() {
// Hibernate operations
}
Caching
Enable Second-Level Cache:
<property name="hibernate.cache.use_second_level_cache">true</property>
Cache Annotations (Entity):
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class YourEntity {
// Entity details
}
Lazy Loading
Lazy Loading (Fetch Type):
@OneToMany(fetch = FetchType.LAZY)
Logging
Hibernate Logging:
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
Batch Processing
Batch Processing:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
for (int i = 0; i < entities.size(); i++) {
session.save(entities.get(i));
if (i % batchSize == 0) {
session.flush();
session.clear();
}
}
transaction.commit();
session.close();
This Hibernate cheat sheet provides a quick reference for common operations and configurations. Adjust the examples based on your specific project requirements and Hibernate version.