[FIX] fix @ManyToOne (add UUID test)

This commit is contained in:
Edouard DUPIN 2024-06-01 13:07:58 +02:00
parent f3a9ebf5e1
commit dc6eeac008
9 changed files with 166 additions and 29 deletions

View File

@ -250,6 +250,14 @@ public class DataAccess {
return out; return out;
} }
public static UUID getListOfRawUUID(final ResultSet rs, final int iii) throws SQLException, DataAccessException {
final byte[] elem = rs.getBytes(iii);
if (rs.wasNull()) {
return null;
}
return UuidUtils.asUuid(elem);
}
protected static <T> void setValuedb( protected static <T> void setValuedb(
final Class<?> type, final Class<?> type,
final T data, final T data,

View File

@ -238,7 +238,11 @@ public class AddOnManyToOne implements DataAccessAddOn {
if (dataNew != null && countNotNull.value != 0) { if (dataNew != null && countNotNull.value != 0) {
field.set(data, dataNew); field.set(data, dataNew);
} }
} else { return;
}
final Field remotePrimaryKeyField = AnnotationTools.getFieldOfId(objectClass);
final Class<?> remotePrimaryKeyType = remotePrimaryKeyField.getType();
if (remotePrimaryKeyType == Long.class) {
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ... // here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
final Long foreignKey = rs.getLong(count.value); final Long foreignKey = rs.getLong(count.value);
count.inc(); count.inc();
@ -254,6 +258,22 @@ public class AddOnManyToOne implements DataAccessAddOn {
}; };
lazyCall.add(lambda); lazyCall.add(lambda);
} }
} else if (remotePrimaryKeyType == UUID.class) {
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
final UUID foreignKey = DataAccess.getListOfRawUUID(rs, count.value);
count.inc();
if (foreignKey != null) {
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
final LazyGetter lambda = () -> {
// TODO: update to have get with abstract types ....
final Object foreignData = DataAccess.get(decorators.targetEntity(), foreignKey);
if (foreignData == null) {
return;
}
field.set(data, foreignData);
};
lazyCall.add(lambda);
}
} }
} }
} }

View File

@ -22,6 +22,9 @@ import org.slf4j.LoggerFactory;
import test.kar.archidata.model.TypeManyToOneRemote; import test.kar.archidata.model.TypeManyToOneRemote;
import test.kar.archidata.model.TypeManyToOneRoot; import test.kar.archidata.model.TypeManyToOneRoot;
import test.kar.archidata.model.TypeManyToOneRootExpand; import test.kar.archidata.model.TypeManyToOneRootExpand;
import test.kar.archidata.model.TypeManyToOneUUIDRemote;
import test.kar.archidata.model.TypeManyToOneUUIDRoot;
import test.kar.archidata.model.TypeManyToOneUUIDRootExpand;
@ExtendWith(StepwiseExtension.class) @ExtendWith(StepwiseExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ -52,8 +55,9 @@ public class TestManyToOne {
@Test @Test
public void testCreateTable() throws Exception { public void testCreateTable() throws Exception {
final List<String> sqlCommand = DataFactory.createTable(TypeManyToOneRemote.class); final List<String> sqlCommand = DataFactory.createTable(TypeManyToOneRemote.class);
final List<String> sqlCommand2 = DataFactory.createTable(TypeManyToOneRoot.class); sqlCommand.addAll(DataFactory.createTable(TypeManyToOneRoot.class));
sqlCommand.addAll(sqlCommand2); sqlCommand.addAll(DataFactory.createTable(TypeManyToOneUUIDRoot.class));
sqlCommand.addAll(DataFactory.createTable(TypeManyToOneUUIDRemote.class));
for (final String elem : sqlCommand) { for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem); LOGGER.debug("request: '{}'", elem);
DataAccess.executeSimpleQuery(elem); DataAccess.executeSimpleQuery(elem);
@ -62,7 +66,7 @@ public class TestManyToOne {
@Order(2) @Order(2)
@Test @Test
public void testAddAlements() throws Exception { public void testRemoteLong() throws Exception {
TypeManyToOneRemote remote = new TypeManyToOneRemote(); TypeManyToOneRemote remote = new TypeManyToOneRemote();
remote.data = "remote1"; remote.data = "remote1";
final TypeManyToOneRemote insertedRemote1 = DataAccess.insert(remote); final TypeManyToOneRemote insertedRemote1 = DataAccess.insert(remote);
@ -119,4 +123,63 @@ public class TestManyToOne {
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData); Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
Assertions.assertNull(retrieve2.remote); Assertions.assertNull(retrieve2.remote);
} }
@Order(3)
@Test
public void testRemoteUUID() throws Exception {
TypeManyToOneUUIDRemote remote = new TypeManyToOneUUIDRemote();
remote.data = "remote1";
final TypeManyToOneUUIDRemote insertedRemote1 = DataAccess.insert(remote);
Assertions.assertEquals(insertedRemote1.data, remote.data);
remote = new TypeManyToOneUUIDRemote();
remote.data = "remote2";
final TypeManyToOneUUIDRemote insertedRemote2 = DataAccess.insert(remote);
Assertions.assertEquals(insertedRemote2.data, remote.data);
final TypeManyToOneUUIDRoot test = new TypeManyToOneUUIDRoot();
test.otherData = "kjhlkjlkj";
test.remoteUuid = insertedRemote2.uuid;
final TypeManyToOneUUIDRoot insertedData = DataAccess.insert(test);
Assertions.assertNotNull(insertedData);
Assertions.assertNotNull(insertedData.uuid);
Assertions.assertEquals(test.otherData, insertedData.otherData);
Assertions.assertEquals(insertedRemote2.uuid, insertedData.remoteUuid);
TypeManyToOneUUIDRoot retrieve = DataAccess.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.uuid);
Assertions.assertEquals(insertedData.uuid, retrieve.uuid);
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
Assertions.assertEquals(insertedRemote2.uuid, retrieve.remoteUuid);
TypeManyToOneUUIDRootExpand retrieve2 = DataAccess.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
Assertions.assertNotNull(retrieve2);
Assertions.assertNotNull(retrieve2.uuid);
Assertions.assertEquals(insertedData.uuid, retrieve2.uuid);
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
Assertions.assertNotNull(retrieve2.remote);
Assertions.assertEquals(insertedRemote2.uuid, retrieve2.remote.uuid);
Assertions.assertEquals(insertedRemote2.data, retrieve2.remote.data);
// remove values:
final int count = DataAccess.delete(TypeManyToOneUUIDRemote.class, remote.uuid);
Assertions.assertEquals(1, count);
// check fail:
retrieve = DataAccess.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.uuid);
Assertions.assertEquals(insertedData.uuid, retrieve.uuid);
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
Assertions.assertEquals(insertedRemote2.uuid, retrieve.remoteUuid);
retrieve2 = DataAccess.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
Assertions.assertNotNull(retrieve2);
Assertions.assertNotNull(retrieve2.uuid);
Assertions.assertEquals(insertedData.uuid, retrieve2.uuid);
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
Assertions.assertNull(retrieve2.remote);
}
} }

View File

@ -1,16 +1,14 @@
package test.kar.archidata.model; package test.kar.archidata.model;
import jakarta.persistence.Column; import org.kar.archidata.model.GenericData;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
public class TypeManyToOneRemote { public class TypeManyToOneRemote extends GenericData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true)
public Long id = null;
public String data; public String data;
@Override
public String toString() {
return "TypeManyToOneRemote [data=" + this.data + ", id=" + this.id + "]";
}
} }

View File

@ -1,20 +1,22 @@
package test.kar.archidata.model; package test.kar.archidata.model;
import org.kar.archidata.model.GenericData;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
public class TypeManyToOneRoot { public class TypeManyToOneRoot extends GenericData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true)
public Long id = null;
public String otherData; public String otherData;
@ManyToOne(targetEntity = TypeManyToOneRemote.class) @ManyToOne(targetEntity = TypeManyToOneRemote.class)
@Column(nullable = false) @Column(nullable = false)
public Long remoteId; public Long remoteId;
@Override
public String toString() {
return "TypeManyToOneRoot [otherData=" + this.otherData + ", remoteId=" + this.remoteId + ", id=" + this.id
+ "]";
}
} }

View File

@ -1,23 +1,25 @@
package test.kar.archidata.model; package test.kar.archidata.model;
import org.kar.archidata.model.GenericData;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.FetchType; import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table; import jakarta.persistence.Table;
@Table(name = "TypeManyToOneRoot") @Table(name = "TypeManyToOneRoot")
public class TypeManyToOneRootExpand { public class TypeManyToOneRootExpand extends GenericData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true)
public Long id = null;
public String otherData; public String otherData;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = TypeManyToOneRemote.class) @ManyToOne(fetch = FetchType.LAZY, targetEntity = TypeManyToOneRemote.class)
@Column(name = "remoteId", nullable = false) @Column(name = "remoteId", nullable = false)
public TypeManyToOneRemote remote; public TypeManyToOneRemote remote;
@Override
public String toString() {
return "TypeManyToOneRootExpand [otherData=" + this.otherData + ", remote=" + this.remote + ", id=" + this.id
+ "]";
}
} }

View File

@ -0,0 +1,9 @@
package test.kar.archidata.model;
import org.kar.archidata.model.UUIDGenericData;
public class TypeManyToOneUUIDRemote extends UUIDGenericData {
public String data;
}

View File

@ -0,0 +1,17 @@
package test.kar.archidata.model;
import java.util.UUID;
import org.kar.archidata.model.UUIDGenericData;
import jakarta.persistence.Column;
import jakarta.persistence.ManyToOne;
public class TypeManyToOneUUIDRoot extends UUIDGenericData {
public String otherData;
@ManyToOne(targetEntity = TypeManyToOneUUIDRemote.class)
@Column(nullable = false)
public UUID remoteUuid;
}

View File

@ -0,0 +1,18 @@
package test.kar.archidata.model;
import org.kar.archidata.model.UUIDGenericData;
import jakarta.persistence.Column;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Table(name = "TypeManyToOneUUIDRoot")
public class TypeManyToOneUUIDRootExpand extends UUIDGenericData {
public String otherData;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = TypeManyToOneUUIDRemote.class)
@Column(name = "remoteUuid", nullable = false)
public TypeManyToOneUUIDRemote remote;
}