forked from jakartaee/persistence
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
second solution for result set mappings
- Loading branch information
Showing
13 changed files
with
168 additions
and
73 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
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
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
22 changes: 22 additions & 0 deletions
22
api/src/main/java/jakarta/persistence/sql/ColumnMapping.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,22 @@ | ||
package jakarta.persistence.sql; | ||
|
||
import jakarta.persistence.ColumnResult; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
public record ColumnMapping<T>(String name, Class<T> type) | ||
implements ColumnResult, MappingElement<T> { | ||
|
||
public static ColumnMapping<Object> map(String name) { | ||
return new ColumnMapping<>(name, Object.class); | ||
} | ||
|
||
public static <T> ColumnMapping<T> map(String name, Class<T> type) { | ||
return new ColumnMapping<>(name, type); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> annotationType() { | ||
return ColumnResult.class; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
api/src/main/java/jakarta/persistence/sql/ConstructorMapping.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,19 @@ | ||
package jakarta.persistence.sql; | ||
|
||
import jakarta.persistence.ColumnResult; | ||
import jakarta.persistence.ConstructorResult; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
public record ConstructorMapping<T>(Class<T> targetClass, ColumnResult[] columns) | ||
implements ConstructorResult, MappingElement<T> { | ||
|
||
public static <T> ConstructorMapping<T> map(Class<T> targetClass, ColumnResult... columns) { | ||
return new ConstructorMapping<>(targetClass, columns); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> annotationType() { | ||
return ConstructorResult.class; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
api/src/main/java/jakarta/persistence/sql/EntityMapping.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,31 @@ | ||
package jakarta.persistence.sql; | ||
|
||
import jakarta.persistence.EntityResult; | ||
import jakarta.persistence.FieldResult; | ||
import jakarta.persistence.LockModeType; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
public record EntityMapping<T>(Class<T> entityClass, LockModeType lockMode, String discriminatorColumn, FieldResult[] fields) | ||
implements EntityResult, MappingElement<T> { | ||
|
||
@SafeVarargs | ||
public static <T> EntityMapping<T> map(Class<T> entityClass, FieldMapping<T>... fields) { | ||
return new EntityMapping<>(entityClass, LockModeType.NONE, "", fields); | ||
} | ||
|
||
@SafeVarargs | ||
public static <T> EntityMapping<T> map(Class<T> entityClass, String discriminatorColumn, FieldMapping<T>... fields) { | ||
return new EntityMapping<>(entityClass, LockModeType.NONE, discriminatorColumn, fields); | ||
} | ||
|
||
@SafeVarargs | ||
public static <T> EntityMapping<T> map(Class<T> entityClass, LockModeType lockMode, String discriminatorColumn, FieldMapping<T>... fields) { | ||
return new EntityMapping<>(entityClass, lockMode, discriminatorColumn, fields); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> annotationType() { | ||
return EntityResult.class; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
api/src/main/java/jakarta/persistence/sql/FieldMapping.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,20 @@ | ||
package jakarta.persistence.sql; | ||
|
||
import jakarta.persistence.FieldResult; | ||
import jakarta.persistence.metamodel.SingularAttribute; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
public record FieldMapping<T>(String name, String column) | ||
implements FieldResult { | ||
|
||
public static <T> FieldMapping<T> map(SingularAttribute<T,?> attribute, String column) { | ||
return new FieldMapping<>(attribute.getName(), column); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> annotationType() { | ||
return FieldResult.class; | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
api/src/main/java/jakarta/persistence/sql/MappingElement.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,17 @@ | ||
package jakarta.persistence.sql; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
|
||
public interface MappingElement<T> { | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <R> R[] newArrayInstance(Class<R> type, int length) { | ||
return (R[]) Array.newInstance(type, length); | ||
} | ||
static <R> R[] extract(Class<R> type, MappingElement[] mappingElements) { | ||
return Arrays.stream(mappingElements) | ||
.filter(type::isInstance) | ||
.toArray(length -> newArrayInstance(type, length)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
api/src/main/java/jakarta/persistence/sql/ResultSetMapping.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,48 @@ | ||
package jakarta.persistence.sql; | ||
|
||
import jakarta.persistence.ColumnResult; | ||
import jakarta.persistence.ConstructorResult; | ||
import jakarta.persistence.EntityResult; | ||
import jakarta.persistence.SqlResultSetMapping; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import static jakarta.persistence.sql.MappingElement.extract; | ||
|
||
public record ResultSetMapping<T>(String name, EntityResult[] entities, ConstructorResult[] classes, ColumnResult[] columns) | ||
implements SqlResultSetMapping { | ||
|
||
public static ResultSetMapping<Object[]> create(MappingElement<?>... mappings) { | ||
return new ResultSetMapping<>("", | ||
extract(EntityResult.class, mappings), | ||
extract(ConstructorResult.class, mappings), | ||
extract(ColumnResult.class, mappings)); | ||
} | ||
|
||
public static <T> ResultSetMapping<T> create(EntityMapping<T> entityMapping) { | ||
return new ResultSetMapping<>("", | ||
new EntityResult[]{entityMapping}, | ||
new ConstructorResult[0], | ||
new ColumnResult[0]); | ||
} | ||
|
||
public static <T> ResultSetMapping<T> create(ConstructorMapping<T> constructorMapping) { | ||
return new ResultSetMapping<>("", | ||
new EntityResult[0], | ||
new ConstructorResult[]{constructorMapping}, | ||
new ColumnResult[0]); | ||
} | ||
|
||
public static <T> ResultSetMapping<T> create(ColumnMapping<T> columnResult) { | ||
return new ResultSetMapping<>("", | ||
new EntityResult[0], | ||
new ConstructorResult[0], | ||
new ColumnResult[]{columnResult}); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> annotationType() { | ||
return SqlResultSetMapping.class; | ||
} | ||
} | ||
|
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