Skip to content

Commit

Permalink
simple entity return
Browse files Browse the repository at this point in the history
  • Loading branch information
firestar committed Sep 1, 2024
1 parent 8dc4d34 commit 8290c33
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencyManagement {
}
}
group = 'com.nucleodb'
version = '3.5.2'
version = '3.5.3'

repositories {
mavenCentral()
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/nucleodb/spring/NDBRepositoryFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nucleodb.library.NucleoDB;
import com.nucleodb.library.database.tables.table.DataEntry;
import com.nucleodb.library.database.utils.Serializer;
import com.nucleodb.spring.impl.NDBConnectionRepositoryImpl;
import com.nucleodb.spring.impl.NDBDataEntryRepositoryImpl;
import com.nucleodb.spring.mapping.NDBEntityInformation;
import com.nucleodb.spring.query.MappingNDBEntityInformation;
import com.nucleodb.spring.query.QueryParser;
import com.nucleodb.spring.query.exec.NDBDataEntryRepositoryQuery;
import com.nucleodb.spring.types.NDBConnRepository;
import com.nucleodb.spring.types.NDBDataRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.NamedQueries;
Expand Down Expand Up @@ -54,9 +58,8 @@ public NDBRepositoryFactory(NucleoDB nucleoDB, ApplicationEventPublisher publish
}

@Override
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
//return new MappingNDBEntityInformation();
return null;
public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> entityClass) {
return new NDBEntityInformation<>(entityClass, (Class<ID>) String.class);
}

@Override
Expand Down
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;
}
}

0 comments on commit 8290c33

Please sign in to comment.