gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:48:31 26 4
gpt4 key购买 nike

我有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com