Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/arey/hibernate-hydrate

Avoid Hibernate LazyInitializationException by recursively resolving proxy
https://github.com/arey/hibernate-hydrate

hibernate

Last synced: 2 days ago
JSON representation

Avoid Hibernate LazyInitializationException by recursively resolving proxy

Awesome Lists containing this project

README

        

# Hibernate Hydrate #

[![Build Status](https://github.com/arey/hibernate-hydrate/actions/workflows/build.yml/badge.svg)](https://github.com/arey/hibernate-hydrate/actions/workflows/build.yml)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.javaetmoi.core/javaetmoi-hibernate6-hydrate/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.javaetmoi.core/javaetmoi-hibernate6-hydrate)

The primary goal of the [Hibernate Hydrate](https://github.com/arey/hibernate-hydrate) project is to populate a graph of persistent entities and thus avoid the famous [LazyInitializationException](http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/LazyInitializationException.html).

## Features ##

* Utility class to populate a lazy-initialized object graph by recursivity
* Supports JPA with Hibernate as provider
* Supports Hibernate 3.x to 6.x (with different artefactId)

## Getting Help ##

This readme file as well as the [wiki](https://github.com/arey/hibernate-hydrate/wiki) are the best places to start learning about Hibernate Hydrate.
There are also unit tests available to look at.

The [wiki](https://github.com/arey/hibernate-hydrate/wiki) contains links to basic project information such as source code, jenkins build, javadocs, issue tracking, etc.

A French article titled *Say goodbye to LazyInitializationException* : http://javaetmoi.com/2012/03/hibernate-dites-adieu-aux-lazy-initialization-exception/

## Quick Start ##

### Dependency ###

Download the jar though Maven:

```xml

com.javaetmoi.core
javaetmoi-hibernate6-hydrate
6.3.4

com.javaetmoi.core
javaetmoi-hibernate5-hydrate
5.2.3

com.javaetmoi.core
javaetmoi-hibernate5-hydrate
2.3

com.javaetmoi.core
javaetmoi-hibernate4-hydrate
2.2

com.javaetmoi.core
javaetmoi-hibernate3-hydrate
2.2

```

Please note that we are not able to support Hibernate versions 6.0 up to 6.1.5 due to bugs in them.

Hibernate Hydrate artefacts are available from [Maven Central](https://repo1.maven.org/maven2/com/javaetmoi/core/javaetmoi-hibernate6-hydrate/)

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.javaetmoi.core/javaetmoi-hibernate6-hydrate/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.javaetmoi.core/javaetmoi-hibernate6-hydrate)

### Usage ###

First get a `Hydrator` instance. There are four possible ways:
* `Hydrator.hydrator(entityManagerFactory)`
* `Hydrator.hydrator(entityManager)`
* `Hydrator.hydrator(sessionFactory)`
* `Hydrator.hydrator(session)`

To this `Hydrator` instance you can pass **attached** entities that need to be fully initialized.
Afterward you can detach these entities and access all of their transitive attributes
without getting problems with lazy loading. That is, there will be no `LazyInitializationException`.

A simple example of a service method that returns a fully initialized entity
except for its `mySubEntities` attribute:

```java
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import com.javaetmoi.core.persistence.hibernate.Hydrator;
import static com.javaetmoi.core.persistence.hibernate.Hydrator.hydrator;

public class MyEntityService {
private final EntityManager entityManager;
private final Hydrator hydrator;

public MyEntityService(EntityManager entityManager) {
this.entityManager = entityManager;
this.hydrator = hydrator(entityManager).withExclude(MyEntity.class, "mySubEntities");
}

@Transactional
public MyEntity myEntity(long id) {
var myEntity = entityManager.find(MyEntity.class, id);
var myHydratedEntity = hydrator.deepHydrate(myEntity);
return myHydrateEntity;
}
}
```

## Contributing to Hibernate Hydrate ##

* GitHub is for social coding platform: if you want to write code, we encourage contributions through pull requests from [forks of this repository](http://help.github.com/forking/).
If you want to contribute code this way, please reference a GitHub ticket as well covering the specific issue you are addressing.
* Each major version of Hibernate has it own git branch:
* Hibernate 6.2 on the master
* Hibernate 5 on the hibernate5 branch
* Hibernate 4 on the hibernate4 branch
* Hibernate 3 on the hibernate3 branch

### Development environment installation ###

Download the code with git:

``git clone git://github.com/arey/hibernate-hydrate.git``

Compile the code with maven:

``mvn clean install``

If you're using an IDE that supports Maven-based projects (IntelliJ Idea, Netbeans or m2Eclipse), you can import the project directly from its POM.
Otherwise, generate IDE metadata with the related IDE maven plugin:

``mvn eclipse:clean eclipse:eclipse``

## Release

This project artefact is published to Maven Central.
The Maven Release Plugin is used to release the project with Maven.
The [release.yml](https://github.com/arey/hibernate-hydrate/actions/workflows/release.yml) GitHub Actions workflow automates the process.

## Credits ##

* Uses [Maven](http://maven.apache.org/) as a build tool
* Uses [GitHub Actions](https://github.com/features/actions) for continuous integration builds whenever code is pushed into GitHub
* [Izaak (John) Alpert](https://github.com/karlhungus) and [Marc Cobery](https://github.com/mcobery) and [Markus Heiden](https://github.com/markusheiden) for their pull requests

## Build Status ##

GitHub Actions: [![Java CI](https://github.com/arey/hibernate-hydrate/actions/workflows/build.yml/badge.svg)](https://github.com/arey/hibernate-hydrate/actions/workflows/build.yml)