https://github.com/cepr0/optional-enum-issue
Spring Data JPA repository method with native query, that should return Optional<Enum>, instead return Optional<String>
https://github.com/cepr0/optional-enum-issue
enum optional spring-boot spring-data-jpa
Last synced: 10 months ago
JSON representation
Spring Data JPA repository method with native query, that should return Optional<Enum>, instead return Optional<String>
- Host: GitHub
- URL: https://github.com/cepr0/optional-enum-issue
- Owner: Cepr0
- Created: 2018-05-05T07:11:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-05T18:42:20.000Z (over 7 years ago)
- Last Synced: 2025-01-17T22:09:21.936Z (12 months ago)
- Topics: enum, optional, spring-boot, spring-data-jpa
- Language: Java
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
Spring Data JPA repository method with native query, that should return **Optional<Enum>**, instead return **Optional<String>**.
Spring Boot - 2.0.1
Spring Data JPA - 2.0.6
**Entity**
```java
@Entity
public class Model {
@Id
private Integer id;
private String name;
@Enumerated(STRING)
@Column(length = 8, nullable = false)
private ModelType type;
}
```
**Repo methods**
```java
@Query(value = "select m.type as type from model m where m.id = ?1", nativeQuery = true)
Optional getOptionalModelTypeById_Native(Integer id);
@Query("select m.type as type from Model m where m.id = ?1")
Optional getOptionalModelTypeById_Jpa(Integer id);
```
**Tests**
```java
@Test
public void getOptionalModelTypeById_Native() {
Optional typeOptional = modelRepo.getOptionalModelTypeById_Native(1);
assertThat(typeOptional).isNotEmpty();
assertThat(typeOptional.get()).isOfAnyClassIn(ModelType.class);
}
@Test
public void getOptionalModelTypeById_Jpa() {
Optional typeOptional = modelRepo.getOptionalModelTypeById_Jpa(1);
assertThat(typeOptional).isNotEmpty();
assertThat(typeOptional.get()).isOfAnyClassIn(ModelType.class);
}
```
Test result for `getOptionalModelTypeById_Native` method:
```
java.lang.AssertionError:
Expecting:
<"TYPE1">
to be of one these types:
<[io.github.cepr0.demo.ModelType]>
but was:
```
Full tests are in `io.github.cepr0.demo.ModelRepoTest`.
The issue [DATAJPA-1338](https://jira.spring.io/browse/DATAJPA-1338) is posted.