【发布时间】:2019-12-31 11:58:51
【问题描述】:
我正在使用 Oracle db 18c 上的 HR 模式编写示例应用程序。
我正在使用 Spring Boot 2、Spring Data Jpa 和 Spring Rest。
我正在处理Regions'表(包含两个字段:region_id 和region_name)和countries 表(包含三个字段:country_id、country_name 和region_id)。
如果 Regions 的实体不包含 @OneToMany 与 Country 的实体的关系,我可以管理所有 CRUD 操作,当我添加它时,应用程序会返回一个 415 错误(不支持的方法),这是没有意义的! 这是我的代码:
区域实体:
package it.aesys.springhr.entities;
import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonBackReference;
import java.util.List;
/**
* The persistent class for the REGIONS database table.
*
*/
@Entity
@Table(name="REGIONS")
@NamedQuery(name="Region.findAll", query="SELECT r FROM Region r")
public class Region {
@Id
@GeneratedValue(generator="REGIONS_SEQ", strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="REGIONS_SEQ", sequenceName="REGIONS_SEQ", allocationSize=0)
@Column(name="REGION_ID", unique=true, nullable=false)
private int regionId;
@Column(name="REGION_NAME", nullable=false, length=50)
private String regionName;
//bi-directional many-to-one association to Country
@OneToMany(mappedBy="region", cascade = CascadeType.ALL)
// this annotation help me to not retrieve all countries when I find all Regions but I tried also without it and anything change
@JsonBackReference
private List<Country> countries;
// constructor, getters and setters as usual
}
国家实体:
package it.aesys.springhr.entities;
import javax.persistence.*;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import java.util.List;
/**
* The persistent class for the COUNTRIES database table.
*
*/
@Entity
@Table(name="COUNTRIES")
@NamedQuery(name="Country.findAll", query="SELECT c FROM Country c")
public class Country {
@Id
@Column(name="COUNTRY_ID", unique=true, nullable=false, length=2)
private String countryId;
@Column(name="COUNTRY_NAME", nullable=false, length=40)
private String countryName;
//bi-directional many-to-one association to Region
@ManyToOne
@JoinColumn(name="REGION_ID")
@JsonManagedReference
private Region region;
// constructor, getters and setters as usual
}
RegionRepository 接口很简单:
package it.aesys.springhr.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import it.aesys.springhr.entities.Region;
public interface RegionRepository extends JpaRepository<Region, Integer> {
}
RegionService 包含此方法:
public void save(Region theRegion) {
regionRepository.save(theRegion);
}
最后 RegionRestController 包含这个方法:
@PostMapping( value= "/regions", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
public Region addRegion(@RequestBody Region theRegion) {
// also just in case they pass an id in JSON ... set id to 0
// this is to force a save of new item ... instead of update
theRegion.setRegionId(0);
regionService.save(theRegion);
return theRegion;
}
我正在使用 CURL 来测试这个应用程序,我尝试以不同的方式通过国家/地区,但没有一种方法有效!
我可以在不使用 Map Struct 等外部框架的情况下解决问题吗?或者,在这种情况下,我必须创建一个新对象,将我收到的内容与我必须坚持的内容进行映射?
[编辑]:我用这段代码修改了最后一个方法: @PostMapping( 值 = "/regions", 消耗 = "application/json;charset=UTF-8", 产生 = "application/json;charset=UTF-8") 公共区域 addRegion(@RequestBody HashMap) { 区域 theRegion= new Region(); theRegion.setRegionId(0); theRegion.setRegionName(map.get("regionName"));
regionService.save(theRegion);
return theRegion;
}
现在它可以工作了,但我不确定这个解决方案是否安全,因为它看起来很简单也很通用......
【问题讨论】:
-
您能发布您发送的确切 curl 请求吗?
-
当然@Springer-F 这里是: curl -H "Content-Type: application/json" -d "@request.json" localhost:8080/hr/api/regions 并且对象从 {"regionId": 0,"regionName":"abc"} 到 {"regionId":0,"regionName":"abc","countries":[]} 传递 {"regionId":0,"regionName":"abc", “国家”:null}
-
你的意思是它可以在没有国家密钥的情况下工作?一旦添加它就会失败?尝试不带文件发送:
curl -H "Content-Type: application/json" -d '{"regionId":0,"regionName":"abc","countries":[]}' "localhost:8080/hr/api/regions" -
没什么变化@Spring-F :-(
-
抱歉,如果没有国家/地区键,它可以工作吗?
标签: java spring rest jpa spring-data-jpa