-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/nucleodb/spring/mapping/NDBEntityInformation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.nucleodb.spring.mapping; | ||
|
||
import org.springframework.data.repository.core.EntityInformation; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class NDBEntityInformation<T, ID> implements EntityInformation<T, ID> { | ||
|
||
private final Class<T> entityClass; | ||
private final Class<ID> idClass; | ||
|
||
public NDBEntityInformation(Class<T> entityClass, Class<ID> idClass) { | ||
this.entityClass = entityClass; | ||
this.idClass = idClass; | ||
} | ||
|
||
@Override | ||
public boolean isNew(T entity) { | ||
// Assuming that if the ID is null, the entity is new | ||
return getId(entity) == null; | ||
} | ||
|
||
@Override | ||
public ID getId(T entity) { | ||
try { | ||
Field idField = entityClass.getDeclaredField("id"); | ||
if(idField == null) { | ||
idField = entityClass.getDeclaredField("uuid"); | ||
} | ||
idField.setAccessible(true); | ||
return (ID) idField.get(entity); | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
throw new IllegalStateException("Could not retrieve id field", e); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public Class<ID> getIdType() { | ||
return idClass; | ||
} | ||
|
||
@Override | ||
public Class<T> getJavaType() { | ||
return entityClass; | ||
} | ||
} |