Comments (3)
If you would like us to spend some time helping you to diagnose the problem, please spend some time describing it and, ideally, providing a minimal yet complete sample that reproduces the problem.
You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
from spring-data-jpa.
Sure, I will provide more details:
in my PostgreSQL database I store posts. One of the columns is created_at
which is of type timestamptz
. For this in Spring Boot I have entity class with projection class:
interface PostProjection {
// Other functions
@JsonProperty("createdAt")
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSX")
fun getCreated_at(): OffsetDateTime
}
@Entity
@Table(name = "posts")
data class Post(
// Other variables
@NotBlank
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSX")
val createdAt: OffsetDateTime = OffsetDateTime.now()
)
To get the data from the database I use repository interface:
@Repository
interface PostRepository : JpaRepository<Post, Long> {
@Query(
value = "SELECT p.* " +
"FROM posts AS p " +
"LEFT JOIN users AS u ON p.user_id = u.id " +
"GROUP BY p.id ",
countQuery = "SELECT COUNT(*) " +
"FROM posts AS p " +
"LEFT JOIN users AS u ON p.user_id = u.id " +
"GROUP BY p.id ",
nativeQuery = true
)
fun <T> findPosts(
cursorBasedPageable: CursorBasedPageable<T>,
pageable: Pageable
): Page<Post>
@Query(
value = "SELECT p.*, SUM(u.reporting_score) " +
"FROM posts AS p " +
"LEFT JOIN users AS u ON p.user_id = u.id " +
"GROUP BY p.id ",
countQuery = "SELECT COUNT(*) " +
"FROM posts AS p " +
"LEFT JOIN users AS u ON p.user_id = u.id " +
"GROUP BY p.id ",
nativeQuery = true
)
fun <T> findPostsWithReportingScore(
cursorBasedPageable: CursorBasedPageable<T>,
pageable: Pageable
): Page<PostProjection>
}
When I call findPosts()
function I do receive all information, including created_at
field which in database is stored as shown below (one of the examples):
The Spring JPA generates the following JSON response (one of the examples):
"createdAt": "2024-03-26T16:10:07.688571Z",
, which is correct.
Now when I call findPostsWithReportingScore()
function I receive a warning: Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Cannot project java.time.Instant to java.time.OffsetDateTime; Target type is not an interface and no matching Converter found]
.
from spring-data-jpa.
Returning Instant
is a consequence of Hibernate's TimestampUtcAsJdbcTimestampJdbcType
class that converts Timestamp
values into Instant
.
Spring Data uses tuple-queries to provide data for interface projections hence we have to rely on the types that Hibernate provides to us.
With the source value being Instant
we do not have sufficient information to convert the value into OffsetDateTime
.
from spring-data-jpa.
Related Issues (20)
- I'm curious about why SimpleJpaRepository checks twice whether the entity is in a persistent state when calling the delete method. HOT 14
- Query Generation Regression when using EntityGraphs on Version 3.2.3 and Hibernate 6.4.4.Final HOT 7
- Cannot run/debug tests with IntelliJ IDEA HOT 1
- Creation Timestamp updating in result but when I get from database nothing changes. HOT 1
- Multi-Tenancy with Spring boot(2.7.18) + Hibernate: "SessionFactory configured for multi-tenancy, but no tenant identifier specified" HOT 2
- Calling Postgres Function From Spring data jpa repository HOT 1
- Align OffsetScrolling to zero-based indexes
- Problem with Spring Data JPA with projections, specifications and SPEL HOT 3
- Repositories do not support collections mapped to SQL arrays HOT 1
- Suboptimal specification queries for object arrays HOT 5
- Broaden integration test runs against Hibernate 6
- Projections with findBy select can't / doesn't trim fields
- Spring Data JPA creates a query according to method name regardless of existing named query HOT 1
- Release 3.1.12 (2023.0.12)
- Release 3.2.6 (2023.1.6)
- Release 3.3 GA (2024.0.0)
- Derived Method Unneccessarily Joins for filter HOT 1
- Spring Data JPA Query with Class Constructor: org.hibernate.query.SemanticException: Cannot compare ... HOT 4
- Using native Hibernate multitenancy with Spring Data JPA breaks on application boot. HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-jpa.