使用提供的json字符串创建模型

时间:2018-10-13 07:14:22

标签: java json spring-boot

我想在Spring Boot中使用以下json字符串创建模型。

{
  "name": "inox",
  "seatInfo": {
    "A": {
      "numberOfSeats": 10,
      "aisleSeats": [
        0,
        5,
        6,
        9
      ]
    },
    "B": {
      "numberOfSeats": 15,
      "aisleSeats": [
        0,
        5,
        6,
        9
      ]
    },
    "D": {
      "numberOfSeats": 20,
      "aisleSeats": [
        0,
        5,
        6,
        9
      ]
    }
  }
}

到目前为止,我已经做到了。

 import java.util.Map;
 import javax.persistence.*;

 @Entity
 public class Screen {

     @Id
     @GeneratedValue
     private Long id;
     private String name;
     private Map<String, RowInfo> seatInfo;

     //getter and setters
 }


 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import java.util.Arrays;

 @Entity
 public class RowInfo {

     @Id
     @GeneratedValue
     private Long id;
     private int numberOfSeats;
     private Integer aisleSeats[];

     //getters and setters
 }

应用程序启动时出现以下异常。

嵌套的异常是org.hibernate.MappingException:无法确定类型:java.util.Map,在表:屏幕上,对于列:[org.hibernate.mapping.Column(seat_info)]

3 个答案:

答案 0 :(得分:0)

使用HashMap类型(或您喜欢的类型)更改Map界面。

@Entity
public class Screen {

     @Id
     @GeneratedValue
     private Long id;
     private String name;
     private HashMap<String, RowInfo> seatInfo;

     //getter and setters
 }


 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import java.util.Arrays;

 @Entity
 public class RowInfo {

     @Id
     @GeneratedValue
     private Long id;
     private int numberOfSeats;
     private Integer aisleSeats[];

     //getters and setters
 }

答案 1 :(得分:0)

如果您使用的是JPA 2,请添加:

@ElementCollection

private Map<String, RowInfo> seatInfo;

答案 2 :(得分:0)

使用一些JPA 2实现:它添加了@ElementCollection注释,与Hibernate注释类似,完全可以满足您的需求。

@Entity
public class Screen {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @ElementCollection
    private Map<String, RowInfo> seatInfo;
}