-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mapping with embedded field with generic type (#3223)
- Loading branch information
1 parent
4179f17
commit 3341071
Showing
14 changed files
with
209 additions
and
1 deletion.
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
9 changes: 9 additions & 0 deletions
9
data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2BookEntityRepository.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,9 @@ | ||
package io.micronaut.data.jdbc.h2; | ||
|
||
import io.micronaut.data.jdbc.annotation.JdbcRepository; | ||
import io.micronaut.data.model.query.builder.sql.Dialect; | ||
import io.micronaut.data.tck.repositories.embedded.BookEntityRepository; | ||
|
||
@JdbcRepository(dialect = Dialect.H2) | ||
public interface H2BookEntityRepository extends BookEntityRepository { | ||
} |
9 changes: 9 additions & 0 deletions
9
data-jdbc/src/test/java/io/micronaut/data/jdbc/h2/H2HouseEntityRepository.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,9 @@ | ||
package io.micronaut.data.jdbc.h2; | ||
|
||
import io.micronaut.data.jdbc.annotation.JdbcRepository; | ||
import io.micronaut.data.model.query.builder.sql.Dialect; | ||
import io.micronaut.data.tck.repositories.embedded.HouseEntityRepository; | ||
|
||
@JdbcRepository(dialect = Dialect.H2) | ||
public interface H2HouseEntityRepository extends HouseEntityRepository { | ||
} |
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
8 changes: 8 additions & 0 deletions
8
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/BaseEntity.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,8 @@ | ||
package io.micronaut.data.tck.entities.embedded; | ||
|
||
public interface BaseEntity<I, S extends Enum<S>> { | ||
|
||
I id(); | ||
|
||
ResourceEntity<S> resource(); | ||
} |
12 changes: 12 additions & 0 deletions
12
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/BookEntity.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,12 @@ | ||
package io.micronaut.data.tck.entities.embedded; | ||
|
||
import io.micronaut.data.annotation.Id; | ||
import io.micronaut.data.annotation.MappedEntity; | ||
import jakarta.persistence.Embedded; | ||
|
||
@MappedEntity | ||
public record BookEntity( | ||
@Id Long id, | ||
@Embedded ResourceEntity<BookState> resource | ||
) implements BaseEntity<Long, BookState> { | ||
} |
7 changes: 7 additions & 0 deletions
7
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/BookState.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,7 @@ | ||
package io.micronaut.data.tck.entities.embedded; | ||
|
||
public enum BookState { | ||
BORROWED, | ||
READ, | ||
RETURNED | ||
} |
12 changes: 12 additions & 0 deletions
12
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/HouseEntity.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,12 @@ | ||
package io.micronaut.data.tck.entities.embedded; | ||
|
||
import io.micronaut.data.annotation.Id; | ||
import io.micronaut.data.annotation.MappedEntity; | ||
import jakarta.persistence.Embedded; | ||
|
||
@MappedEntity | ||
public record HouseEntity( | ||
@Id Long id, | ||
@Embedded ResourceEntity<HouseState> resource | ||
) implements BaseEntity<Long, HouseState> { | ||
} |
6 changes: 6 additions & 0 deletions
6
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/HouseState.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,6 @@ | ||
package io.micronaut.data.tck.entities.embedded; | ||
|
||
public enum HouseState { | ||
BUILDING, | ||
FINISHED | ||
} |
17 changes: 17 additions & 0 deletions
17
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/ResourceEntity.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 io.micronaut.data.tck.entities.embedded; | ||
|
||
import io.micronaut.data.annotation.Embeddable; | ||
import io.micronaut.data.annotation.TypeDef; | ||
import io.micronaut.data.model.DataType; | ||
import jakarta.persistence.Convert; | ||
|
||
@Embeddable | ||
public record ResourceEntity<S extends Enum<S>>( | ||
|
||
String displayName, | ||
|
||
@TypeDef(type = DataType.STRING) | ||
@Convert(converter = StateConverter.class) | ||
S state | ||
) { | ||
} |
37 changes: 37 additions & 0 deletions
37
data-tck/src/main/java/io/micronaut/data/tck/entities/embedded/StateConverter.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,37 @@ | ||
package io.micronaut.data.tck.entities.embedded; | ||
|
||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Converter; | ||
|
||
@Converter | ||
public class StateConverter<Enum> implements AttributeConverter<java.lang.Enum, String> { | ||
|
||
@Override | ||
public String convertToDatabaseColumn(java.lang.Enum anEnum) { | ||
if (anEnum == null) { | ||
return null; | ||
} | ||
return anEnum.name(); | ||
} | ||
|
||
@Override | ||
public java.lang.Enum convertToEntityAttribute(String string) { | ||
if (string == null) { | ||
return null; | ||
} | ||
// Because enum generics in ResourceEntity then implement this | ||
// simple converter just to be able to run tests | ||
if (string.equals("BORROWED")) { | ||
return BookState.BORROWED; | ||
} else if (string.equals("READ")) { | ||
return BookState.READ; | ||
} else if (string.equals("RETURNED")) { | ||
return BookState.RETURNED; | ||
} else if (string.equals("BUILDING")) { | ||
return HouseState.BUILDING; | ||
} else if (string.equals("FINISHED")) { | ||
return HouseState.FINISHED; | ||
} | ||
throw new IllegalStateException("Unexpected enum value: " + string); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
data-tck/src/main/java/io/micronaut/data/tck/repositories/embedded/BookEntityRepository.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,12 @@ | ||
package io.micronaut.data.tck.repositories.embedded; | ||
|
||
import io.micronaut.data.repository.CrudRepository; | ||
import io.micronaut.data.tck.entities.embedded.BookEntity; | ||
import io.micronaut.data.tck.entities.embedded.BookState; | ||
|
||
import java.util.List; | ||
|
||
public interface BookEntityRepository extends CrudRepository<BookEntity, Long> { | ||
|
||
List<BookEntity> findAllByResourceState(BookState state); | ||
} |
12 changes: 12 additions & 0 deletions
12
...-tck/src/main/java/io/micronaut/data/tck/repositories/embedded/HouseEntityRepository.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,12 @@ | ||
package io.micronaut.data.tck.repositories.embedded; | ||
|
||
import io.micronaut.data.repository.CrudRepository; | ||
import io.micronaut.data.tck.entities.embedded.HouseEntity; | ||
import io.micronaut.data.tck.entities.embedded.HouseState; | ||
|
||
import java.util.List; | ||
|
||
public interface HouseEntityRepository extends CrudRepository<HouseEntity, Long> { | ||
|
||
List<HouseEntity> findAllByResourceState(HouseState state); | ||
} |