Serializing many-to-many relationships with Spring MVC, Jackson, and Hibernate

It’s difficult for me to determine how to correctly identify and annotate models in my web application so that they can be used effectively both in the web interface and in the REST web service. Here is a simplified version of the relationship that is causing me problems:

Publication Model:

@Entity
@Table(name = "POST")
public class Post implements Serializable {

    @Id
    @Column(name = "POST_ID")
    @GeneratedValue(strategy-GenerationType.AUTO)
    private Integer postId;

    @Column(name = "POST_BODY")
    private String postBody;

    @ManyToMany(fetch = FetchType.EAGER)
    @Cascade({CascadeType.SAVE_UPDATE})
    @JoinTable(name = "POST_TAGS", 
            joinColumns={@JoinColumn(name="POST_ID")},
            inverseJoinColumns={@JoinColumn(name="TAG_ID")})
    private Set<Tag> tags = new HashSet<Tag>();

    //Getters and setters...
}

Tag model

@Entity
@Table(name = "TAG")
public class Tag implements Serializable {

    @Id
    @Column(name = "TAG_ID")
    @GeneratedValue(strategy-GenerationType.AUTO)
    private Integer tagId;

    @Column(name = "TAG_NAME")
    private String tagName;

    @ManyToMany(fetch = FetchType.EAGER, mappedBy="tags")
    private Set<Post> posts = new HashSet<Post>();

    //Getters and setters...
}

I have one web services controller method for retrieving all messages and one method for retrieving all tags. Each method should ideally return a list of the target class plus reference classes. For instance:

[{
    postId: 1,
    postBody: "Hello world!",
    tags: [{
        tagId: 1,
        tagName: "hello"
    },{
        tagId: 2,
        tagName: "message"
    }]
}, { 
    postId: 2,
    ....
}]

, , - , , .. . @JsonIgnore getter , . @JsonIdentityInfo , , , . , , , , .

+4
2

Jackson (@JsonManagedReference @JsonBackReference).

http://wiki.fasterxml.com/JacksonFeatureBiDirReferences

, , , Gson Jackson.

0

hibernate jackson (https://github.com/FasterXML/jackson-datatype-hibernate) .

OP post - jackson + jackson-databind - . " ", @JsonIgnore + @JsonIdentityInfo. "ID ", .

- . 1. maven dep

<dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-hibernate4</artifactId>
      <version>${jackson.version}</version>
</dependency>
  1. ObjectMapper

HibernateAwareObjectMapper.java

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;

public class HibernateAwareObjectMapper extends ObjectMapper {

    public HibernateAwareObjectMapper() {
        registerModule(new Hibernate4Module());
    }
}

3. mapper spring XML

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="path.HibernateAwareObjectMapper"/>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
  1. .

.

@JsonIgnore "".

EAGER LAZY fetchtype. ObjectMapper config FORCE_LAZY_LOADING javadoc (http://fasterxml.imtqy.com/jackson-datatype-hibernate/javadoc/h4-2.4/com/fasterxml/jackson/datatype/hibernate4/Hibernate4Module.Feature.html#FORCE_LAZY_LOADING) github

0

Source: https://habr.com/ru/post/1537098/


All Articles