繁体   English   中英

Spring数据jpa和具有某种关系的实体

[英]Spring data jpa and entities with some kind of relationship

我正在 Oracle db 18c 上使用 HR 模式编写示例应用程序。

我正在使用 Spring Boot 2、Spring Data Jpa 和 Spring Rest。 我正在研究Regions的表(包含两个字段: region_idregion_name )和countries表(包含三个字段: country_idcountry_nameregion_id )。

我可以管理 Regions 表上的所有 CRUD 操作,如果它的实体不包含 @OneToMany 与 Country 实体的关系,当我添加它时,应用程序返回给我一个没有意义的 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( value="/regions",consumes = "application/json;charset=UTF-8", products = "application/json;charset=UTF-8" ) 公共区域 addRegion(@RequestBody HashMap) { Region theRegion= new Region(); theRegion.setRegionId(0); theRegion.setRegionName(map.get("regionName"));

    regionService.save(theRegion);

    return theRegion;
}

现在它起作用了,但我不确定这个解决方案是否安全,因为它看起来如此简单和通用......

在编辑时,我使用以下代码修改了最后一个方法:

@PostMapping( value= "/regions", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
public Region addRegion(@RequestBody HashMap<String, String>) {
    Region theRegion= new Region(); 
    theRegion.setRegionId(0);
    theRegion.setRegionName(map.get("regionName"));

    regionService.save(theRegion);

    return theRegion;
}

现在它起作用了,但我不确定这个解决方案是否安全,因为它看起来如此简单和通用,所以如果您认为它不确定,请回答并告诉我另一个更好的解决方案!

您可以在服务类和控制器类中使用@Component在主类中使用@Component @ComponentScan(basePackages = "<full package name>" 。问题可能会得到解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM