使用Spring Data JPA保持一对一的关系不起作用

时间:2014-04-29 07:27:52

标签: java spring rest jpa

我正在使用以下教程Spring Data REST通过Spring提供的REST Api访问数据库条目。

所以现在我得到了以下实体类

产品

@Entity
@Table( name = "products" )
public class ProductsEntity implements Serializable {

@OneToOne(fetch = FetchType.EAGER)
//@JoinColumn(name="Units_id", insertable = false, updatable = false)
@JoinColumn(name="Units_id")    
private UnitsEntity unitsEntity;

@Id
@GeneratedValue
@Column( name = "idproducts" )
private Long idProducts;

@Column(name = "productname" )
private String productName;

@Column (name = "Units_id")
private Long unitsid;

public ProductsEntity(){}

// Getter and setter

}

单元

@Entity
@Table(name = "units")
public class UnitsEntity implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
private Long id;

@Column(name="unit")
private String unit;

@Column(name="units_id")
private Long units_id;

// Getter and Setter
}

正如您所看到的,产品和单位之间存在一对一的关系

现在我可以检索具有正确单位的产品,但我无法通过REST API继续使用它。它说有No suitable HttpMessageConverter found to read request body into object of type class ..db.entities.ProductsEntity from request with content type of application/json!

我是否需要HttpMessageConverter或者我的课程中是否存在错误或缺少的配置元素?!

**编辑:**

JSON请求如下所示(unitsEntity是现有实体):

{
    "productName": "Äpfel12342344",
        "unitsEntity": {
        "unit" : "Anzahl",
        "units_id" : 1
     }
} 

但现在我得到以下异常: "Could not read JSON: (was java.lang.NullPointerException) (through reference chain: ...db.entities.ProductsEntity["unitsEntity"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: ...db.entities.ProductsEntity["unitsEntity"])"

0 个答案:

没有答案