Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这样修改spring数据结果查询是一种好做法吗?_Java_Spring_Spring Data Jpa - Fatal编程技术网

Java 这样修改spring数据结果查询是一种好做法吗?

Java 这样修改spring数据结果查询是一种好做法吗?,java,spring,spring-data-jpa,Java,Spring,Spring Data Jpa,我想听听你对我使用的一种做法的看法,我不认为它很干净 其思想是根据需求添加一个字段,因此是非持久性的。 我留下一个车库的例子,里面有几辆不同品牌和颜色的汽车。请求必须显示车库中的所有车辆,如果颜色与请求的颜色相对应,则计算字段“作为所选颜色””必须显示真或假,如本例所示: { "cars": [ { "color": "red", "brand": "opel", "garage": {

我想听听你对我使用的一种做法的看法,我不认为它很干净

其思想是根据需求添加一个字段,因此是非持久性的。 我留下一个车库的例子,里面有几辆不同品牌和颜色的汽车。请求必须显示车库中的所有车辆,如果颜色与请求的颜色相对应,则计算字段“作为所选颜色””必须显示真或假,如本例所示:

{
    "cars": [
        {
            "color": "red",
            "brand": "opel",
            "garage": {
                "name": "Garage1"
            },
            HERE => "as selected color": true
        },
        {
            "color": "black",
            "brand": "bmw",
            "garage": {
                "name": "Garage1"
            },
            HERE => "as selected color": false
        },
        {
            "color": "red",
            "brand": "fiat",
            "garage": {
                "name": "Garage1"
            },
            HERE => "as selected color": true
        },
        ...
    ]
}
为了得到这个由两个实体的信息组成的结果,这两个实体通过一对一的关系链接在一起,我使用了如下对象列表:

@PostMapping("/list")
public ResponseEntity<HashMap> process(@RequestBody Map<String, String> request) throws Exception {
...    
    List<Object> garageWithCars = new ArrayList<Object>();
    garageWithCars = garageRepository.findCars();

    // Set if color is same as asked
    // Is it a good practice ?
    for (Object tempObject : garageWithCars) {
        if (tempObject.getClass() == Car.class) {
            ((Car)tempObject).setIsSelectedColor(color);
        }
    }

    map.put("List", garageWithCars);
    return new ResponseEntity<HashMap>(map, HttpStatus.OK);
}
车库等级:

@Entity
@Table(name = "car")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "car_id")
    @JsonIgnore
    private Long id;

    @Column(name = "color")
    String color;

    @Column(name = "brand")
    String brand;

    @Transient
    @JsonProperty("as selected color")
    Boolean isSelectedColor = false;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="garage_id", nullable = false)
    private Garage garage;

    Car() {}

    public Boolean getIsSelectedColor() {
        return isSelectedColor;
    }

    public void setIsSelectedColor(String selectedColor) {
        if(color.equalsIgnoreCase(selectedColor)) {
            this.isSelectedColor = true;
        }
    }
    ... getters and setters
}
@Entity
@Table(name="garage")
public class Garage {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "garage_id")
    @JsonIgnore
    private Long id;

    @Column(name="name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "garage")
    @JsonBackReference
    private Set<Car> cars = new HashSet<>();

    public Garage() {}

    ... getters and setters
}
@实体
@表(name=“garage”)
公共级车库{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“garage\u id”)
@杰索尼奥雷
私人长id;
@列(name=“name”)
私有字符串名称;
@OneToMany(级联=级联类型.ALL,
fetch=FetchType.LAZY,
mappedBy=“车库”)
@JsonBackReference
私家车=新的HashSet();
公共车库(){}
…能手和二传手
}
@Entity
@Table(name="garage")
public class Garage {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "garage_id")
    @JsonIgnore
    private Long id;

    @Column(name="name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "garage")
    @JsonBackReference
    private Set<Car> cars = new HashSet<>();

    public Garage() {}

    ... getters and setters
}