Skip to content

Commit

Permalink
Read Detachment Correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
GedMarc committed Feb 17, 2025
1 parent 021a97d commit 7060d08
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
22 changes: 19 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@
<annotationProcessorPath>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<!--<version>${maven.hibernate.version}</version>-->
<version>6.4.8.Final</version>
<version>${maven.hibernate.version}</version>
</annotationProcessorPath>
<path>
<groupId>jakarta.xml.bind</groupId>
Expand All @@ -158,7 +157,24 @@
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${maven.hibernate.enhance.version}</version>
<executions>
<execution>
<configuration>
<enableLazyInitialization>true</enableLazyInitialization>
<!--<enableDirtyTracking>false</enableDirtyTracking>-->
<enableAssociationManagement>false</enableAssociationManagement>
<enableExtendedEnhancement>true</enableExtendedEnhancement>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/entityassist/RootEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.NotNull;
import org.hibernate.Session;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -107,6 +108,23 @@ public Q builder()
}
}

/**
* @return A refreshed entity from the DB with this key
*/
public J refresh()
{
return isDetached() ? builder().getEntityManager().merge((J) this) : (J) this;
}

/**
*
* @return If this entity is currently detached
*/
public boolean isDetached()
{
return builder().getEntityManager().unwrap(Session.class).contains((J) this);
}

/**
* Returns this classes associated query builder class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Session;

import java.io.Serializable;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -242,8 +243,7 @@ public J persist(E entity)
{
if (onCreate(entity))
{
boolean transactionAlreadyStarted = false;
getEntityManager().persist(entity);
getEntityManager().unwrap(Session.class).persist(entity);
entity.setFake(false);
setEntity(entity);
}
Expand Down Expand Up @@ -311,7 +311,7 @@ public E update(E entity) throws SQLException
{
if (onUpdate(entity))
{
getEntityManager().merge(entity);
entity = getEntityManager().merge(entity);
}
}
catch (IllegalStateException ise)
Expand Down

0 comments on commit 7060d08

Please sign in to comment.