简体   繁体   English

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

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

I'm writing a sample app using the HR schema on an Oracle db 18c.我正在 Oracle db 18c 上使用 HR 模式编写示例应用程序。

I'm using Spring boot 2, Spring Data Jpa and Spring Rest.我正在使用 Spring Boot 2、Spring Data Jpa 和 Spring Rest。 I'm working on Regions ' table (that contains two fields: region_id and region_name ) and countries table (that contains three fields: country_id , country_name and region_id ).我正在研究Regions的表(包含两个字段: region_idregion_name )和countries表(包含三个字段: country_idcountry_nameregion_id )。

I can manage all CRUD operation on Regions' table if its entity doesn't contain the relationship @OneToMany with Country's entity when I add it the application return me a 415 error (non supported method) that have no sense!我可以管理 Regions 表上的所有 CRUD 操作,如果它的实体不包含 @OneToMany 与 Country 实体的关系,当我添加它时,应用程序返回给我一个没有意义的 415 错误(不支持的方法)! Here it is my code:这是我的代码:

Region Entity:区域实体:

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
}

Country Entity:国家实体:

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
}

The RegionRepository Interface is simply: 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> {
}

and the RegionService contains this method:并且RegionService包含此方法:

public void save(Region theRegion) {
    regionRepository.save(theRegion);
}

and finally the RegionRestController contains this method:最后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;
}

I'm using CURL to test this app and I tried to pass countries in different ways but no one works!我正在使用 CURL 来测试这个应用程序,我试图以不同的方式通过国家,但没有一个有效!

Can I resolve without using some external framework like Map Struct?我可以在不使用 Map Struct 等外部框架的情况下解决吗? Or, in this case I MUST create a new object that mapping what I receive with what I must persist?或者,在这种情况下,我必须创建一个新对象,将我收到的内容与我必须坚持的内容进行映射?

[Edit]: I modify the last method with this code: @PostMapping( value= "/regions", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8") public Region addRegion(@RequestBody HashMap) { Region theRegion= new Region(); [编辑]:我用以下代码修改了最后一个方法:@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.setRegionId(0); theRegion.setRegionName(map.get("regionName")); theRegion.setRegionName(map.get("regionName"));

    regionService.save(theRegion);

    return theRegion;
}

and now it works but I'm not sure that this solution is secure because it seems so simply and so generic ...现在它起作用了,但我不确定这个解决方案是否安全,因为它看起来如此简单和通用......

As I edited, I modify the last method with this code:在编辑时,我使用以下代码修改了最后一个方法:

@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;
}

and now it works but I'm not sure that this solution is secure because it seems so simply and so generic so if you think that it is not sure please answer and tell me another better solution!现在它起作用了,但我不确定这个解决方案是否安全,因为它看起来如此简单和通用,所以如果您认为它不确定,请回答并告诉我另一个更好的解决方案!

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

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

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