java - 非空属性引用 transient 值 - transient 实例必须在当前操作之前保

我有 2 个域模型和一个 Spring REST Controller ,如下所示:

@Entity
public class Customer{

@Id
private Long id;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;

// other stuff with getters/setters

}

@Entity
public class Country{

@Id
@Column(name="COUNTRY_ID")
private Integer id;

// other stuff with getters/setters

}

Spring REST Controller :

@Controller
@RequestMapping("/shop/services/customers")
public class CustomerRESTController {

   /**
    * Create new customer
    */
    @RequestMapping( method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public com.salesmanager.web.entity.customer.Customer createCustomer(@Valid @RequestBody   Customer customer, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        customerService.saveOrUpdate(customer);

        return customer;
    }

    // other stuff
}

我正在尝试使用以下 JSON 作为正文调用以上 REST 服务:

{
    "firstname": "Tapas",
    "lastname": "Jena",
    "city": "Hyderabad",
    "country": "1"
}

国家/地区代码 1 已在国家/地区表中。问题是当我调用此服务时出现以下错误:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.test.model.Customer.country -> com.test.model.Country; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.test.model.Customer.country -> com.test.model.Country

任何帮助将不胜感激!

最佳答案

试着把 CascadeType.ALL

@OneToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@JoinColumn(name="COUNTRY_ID", nullable=false) 

private Country country;

关于java - 非空属性引用 transient 值 - transient 实例必须在当前操作之前保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19074278/

相关文章:

json - 在 Play Framework JsObject 中解析 Json 数组

json - 我应该如何处理 JSON 中的 HATEOAS 链接和引用?

json - Scala:将 JSON 直接解析为案例类

python - 如何在保留矩阵维度的同时序列化 numpy 数组?

c# - 如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET

java - 在 JAX-RS 2.0 客户端库中处理自定义错误响应

javascript - IE10/11 Ajax XHR 错误 - SCRIPT7002 : XM

json - JSONObject 的 Jackson 2 等价物是什么?

python - 在 View 中强制应用程序/json MIME 类型(Flask)

java - 将 InputStream 转换为 JSONObject