i have a database blog which contains 2 tables one...
# spring
n
i have a database blog which contains 2 tables one is users and posts. inside the posts i have an author_id that reference to users.id. How can i join or mapped this to my posts. right now when i do "/getallposts" it will return this :
Copy code
[ {
  "title" : "Get Started with PostgreSQL",
  "body" : "Use foreign keys to ensure data integrity in Postgres.",
  "authorId" : 3,
  "createdAt" : 1505016000000,
  "id" : 1
} ]
I want the
authorId
to return the actual name of the author/user. How can I do that? PostRepository.kt
Copy code
@Repository
interface PostRepository : CrudRepository<Post, Long>{
    @Query("select posts.*, users.name from posts left join users on users.id = posts.author_id;", nativeQuery = true)
    fun getAllPosts (): String
}
PostController.kt
Copy code
@RequestMapping("/getallposts")
    fun getAllPosts () : String {
        val posts =  repository.getAllPosts()
        return posts
    }
Post.kt
Copy code
@Entity(name = "posts")
data class Post (
        val title: String,
        val body: String,
        val authorId: Int,
        val createdAt: Date = Date(),
        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        val pId: Long = -1) {

    private constructor() : this("", "", -1, Date())

}
i get this error.
Copy code
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 4; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 4] with root cause