如何在springboot中同时插入来自单个json的两个不同表的值?

时间:2017-06-14 06:17:44

标签: java json spring spring-boot spring-data-jpa

我有两个类:client和contant。每个客户端可以有很多联系人。客户端类的结构是

  public class client{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long clientId;
  private String clientName;
  @OneToMany
  @JsonBackReference
  private List<ContactInfo> contactInfoList;
  //default constructor
  //getter and setter
  } 

,联系类是: -

   @Entity
   public class ContactInfo {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long contactId;
   @NotNull
   private String mobilePhone;
   @ManyToOne
   @JsonManagedReference
   private Client client;
   //default constructor
   //getter and setter
   }

获取数据的类是: -

   public interface ContactInfoRepo extends JpaRepository<ContactInfo,Long> {
   }

   public interface clientRepo extends JpaRepository<client,Long> {
   }

    public class ClientController {
    @Autowired
    ClientRepo repo;
    @RequestMapping(method = RequestMethod.POST, path = "api/client")
    public void addClient(@RequestBody Client client)throws 
    ConstraintViolationException {
    System.out.println(client.toString());
    repo.save(client);
    }
    }

这是我想要保存在两个不同表中的json数据;

    {
    "clientName":"test",
     "contactInfoList":[{"mobilePhone":"1234"},{"mobilePhone":"12345"} ]
     }

我希望contactInfoList存储在联系人表中,clientName存放在客户端表中。我在spring Boot中使用JPA。当我发送json值时,contactInfoList为null。有人可以帮忙吗?

0 个答案:

没有答案