2017-02-24 17 views
0

我的项目使用JPARepository框架,这限制了我的调试能力,以查看客户端和服务器之间发送的任何数据实际发生的情况。因此,我很难追查我目前的问题。如何使用JSON POST或放置一组<>链接?

我有类与另一个类包含ManyToMany关系。 Visual Paradigm工具自动生成这两个实体之间的关联表,但不会为该关联表生成Java代码,也不会为该表的直接请求创建存储库。我目前正在使用RestConsole测试一个类,以确定在客户端和服务器之间传递的确切JSON。具体而言,在我与挣扎的类,这里是声明:

@ManyToMany(mappedBy="provider", targetEntity=location.ServiceCoreType.class) 
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE) 
private java.util.Set<location.ServiceCoreType> srvcCoreType = new java.util.HashSet<location.ServiceCoreType>(); 

其他类与其他类多对一的关系,当我邮寄或将数据保存这些数据,我的JSON用于链接类样子:

"facility" : "http://localhost:8080/enums_facility/2" 

当我最后一次在这个项目上两年前工作过,我记得,对象的集合<>我需要的东西,如:

"srvcCoreType" : [ "http://localhost:8080/enums_servicecore/2", "http://localhost:8080/enums_servicecore/3" ] 

然而,当我在POST上使用它时,这些数据似乎被忽略了(即关联表中没有条目),但是该项目的其余部分是在数据库中创建的。如果我做PUT,我得到一个错误:

{"error":"Can not construct instance of location.ServiceCoreType: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/enums_servicecore/2')\n at [Source: N/A; line: -1, column: -1]"} 

我在这里错过了什么?我如何告诉服务器将提供者链接到核心类型?

更新: 我发现VP中的模型可能发生了一些情况,因为它为相关关系生成了不同的代码。然后我取代了代码与这种关系的原代码:

@ManyToMany(targetEntity=com.comporium.domain.location.ServiceCoreType.class) 
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.ALL}) 
@JoinTable(name="ComporiumProvider_ServiceCoreType", joinColumns={ @JoinColumn(name="ComporiumProviderID") }, inverseJoinColumns={ @JoinColumn(name="ServiceCoreTypeId") }) 
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE) 
private java.util.Set<location.ServiceCoreType> srvcCoreType = new java.util.HashSet<location.ServiceCoreType>(); 

我现在可以发布此数据:

{ 
    "id" : 98, 
    "deleted" : "false", 
    "description": "leaver", 
    "modifiedBy" : "Fred" , 
    "name" : "Emca",  
    "srvcCoreType" : [ "http://localhost:8080/enums_servicecore/2" , "http://localhost:8080/enums_servicecore/3" ] 
} 

,这将创建的关联表两个环节。成功!

或者可能不是。我可以用一个或两个不同的链接做PUT或粘贴,并且一切按预期工作。但是,如果我试图把或有更多的联系PASTE比目前存在的,我得到以下错误:

{"error":"Can not construct instance of com.comporium.domain.location.ServiceCoreType: no String-argument constructor/factory method to deserialize from String value ('http://localhost:8080/enums_servicecore/2')\n at [Source: N/A; line: -1, column: -1]"} 

因此,如果供应商在没有任何链接创建的,是没有办法创造任何新的链接,不可能添加新的链接。这可能是一个Hibernate问题,如果是这样,什么Hibernate标志可以解决这个问题。 (我不知道这是否有任何方向,但是原始代码在没有错误的情况下执行 - 而不是创建或更新任何链接 - 直到我PUT或PASTE多于当前存在的链接。如果是这样,我得到的错误与如何知道有多少存在没有@JoinTable注解,并且如果它知道该表,为什么它不更新这些行?)

UPDATE 2: 找到可能导致问题的原因。此功能在生产中正常工作,这是较旧的。

生产配置为Spring Boot v1.2.8.RELEASE/Hibernate v4,其中spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.DefaultNamingStrategy。

我目前的开发是在Spring Boot v1.4.3.RELEASE/Hibernate v5中,spring.jpa.hibernate.naming.physical-strategy = org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl。

难道这能说明不能添加新链接的问题是什么?

回答

0

解决方案结果很简单。在ServiceCoreType类,我添加了一个空的构造:

public ServiceCoreType(String json) { } 

的web服务现在可以按预期/期望。如果增加链接数量,我不确切知道框架背后的框架需要什么,但是我得到了POST,PUT和PATCH所需的结果。