0

Let's say that an author have written many books, and also many books have only one author.

So that it is a bidirectional relationship.

When you try to serialize it, it becomes an infinite loop of books and authors like this :

{
    "id": 1,
    "name": "1984",
    "author": {
        "id": 1,
        "name": "George Orwell",
        "books": [
            {
                "id": 1,
                "name": "1984",
                "author": {
                    "id": 1,
                    "name": "George Orwell",
                    "books": [
                        {
                            "id": 1,
                            "name": "1984",
                            "author": {
                ...etc.

Those two annotations (@JsonManagedReference and @JsonBackReference) help us to break this loop :

@Entity
public class Book {
    
    @ManyToOne
    @JoinColumn(name = "author_id")
    @JsonManagedReference
    private Author author;

}

@Entity
public class Author extends AbstractEntity {
    
    @OneToMany(mappedBy = "author", fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
//  @JsonIgnore
    @JsonBackReference
    private Set<Book> books;

}

But with this solution you can access to the books with their proper author

{
    "id": 1,
    "name": "1984",
    "author": {
        "id": 1,
        "name": "George Orwell"
    }
}

, but you can access to the authors with their books :

{
    "id": 1,
    "name": "George Orwell"
}

Does someone already did fix this problem ? Or it's just the way it is to access the complete information from only one side ?

Thank you for your help

Joss

jozinho22
  • 459
  • 2
  • 7
  • 24
  • 2
    You can't. The type is discarded at run time. – Dawood ibn Kareem Dec 01 '20 at 21:55
  • What exactly do you want to do? – tgdavies Dec 01 '20 at 22:11
  • There is one way - anonymous classes. Consider fasterxml TypeReference, maybe it’s what you need. – ip.java Dec 01 '20 at 22:44
  • Maybe this post helps you https://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection – Octavian R. Dec 01 '20 at 23:10
  • @ipergenitsa not exactly, because at my step i can declare explicitly the Type. – jozinho22 Dec 02 '20 at 10:04
  • @OctavianR. No it's not working, I ve got the same error that the person on the messages below the answer – jozinho22 Dec 02 '20 at 10:05
  • 1
    Of course, you would have to adapt it, to use `getGenericInterfaces()[0]` instead of `getGenericSuperclass()`. But don’t waste your time. [This comment](https://stackoverflow.com/questions/65099099/how-to-get-the-type-of-a-t-parameter#comment115089752_65099099) already nailed it. You can’t. All that Reflection could tell you, is that the actual type argument to the interface is the type parameter `T` of your class. – Holger Dec 02 '20 at 18:56
  • If you really (really!) need this, you have a sever flaw in your design, since a generic type parameter is normally used when the actual type doesn't matter. – Ingo Dec 03 '20 at 07:14

0 Answers0