hibernate entity to json

后端 未结 4 852
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 05:06

i use Hibernate 4 and Spring 3.

i have two entity.

Book entity

@Entity
@Table(name = \"book\")
public class Book implements Serializable {

          


        
相关标签:
4条回答
  • 2021-01-06 05:30

    Try using these two Jackson artifacts instead

    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.9</version>
    </dependency>
    
    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.9</version>
    </dependency>
    

    Also on your controller try by changing it to -

     @SuppressWarnings("unchecked")
    @RequestMapping( method = RequestMethod.GET,  produces = MediaType.APPLICATION_JSON_VALUE )
    public @ResponseBody List<Book> getBook() {
    

    Lastly, make sure your view is making a json request.

    0 讨论(0)
  • 2021-01-06 05:32

    You can use @com.fasterxml.jackson.annotation.JsonIgnore annotation to avoid a field to be serialized. Thus the getter of that field won't get called.

    @JsonIgnore
    @OneToMany( mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Book> books = new HashSet<Book>();
    
    0 讨论(0)
  • 2021-01-06 05:40

    Generally, when you call getter methods of entity classes(which returns relation object) out of transaction, then you get LazyInitializationExceptions.

    That's what might be happening in your case if you are converting entity class objects(retrieved from query) to json out of transaction.

    I had same issue, I converted my entity object retrieved by hibernate to json in controller. As controller was out of transaction(Transaction at service layer), while converting to json, getter methods of entity class objects are called and I got LazyInitializationException. Which obstructed object conversion to json, and response was not returned.

    My solution, Try this :

    @SuppressWarnings("unchecked")
    @RequestMapping( method = RequestMethod.GET )
    public @ResponseBody List<Book> getBook() {
        List<Book> res = bookService.findAll();
        for(Book book : res) {
           book.getAuthor().setBooks(null);
        }
        return res;
    }
    
    0 讨论(0)
  • 2021-01-06 05:41


    As others have suggested, I would really not advise you to try to JSON serialize (or actually perform any serialization) of hibernate entities.
    You must remember that the fetched entities are actually "proxified" objects (Hibernate uses ASM, CGLIB and other "dynamic proxiy" frameworks).
    As a result for example, collections get replaced with [PersistenceBags] which may be initialized "lazily" , and cause you hibernate exceptions 1.

    But the problems do not stop there, you may see issues when trying to serialize an Hibernate custom type
    I know this might sound you like writing "boillerplate" code but you might end up coding DTOs - data transfer objects which will take the entity returned from your DAL, and transform them to an object that can be serialized.

    You can use a framework like dozer in order to ease development of serialization between an entity to a DTO.

    0 讨论(0)
提交回复
热议问题