嵌入对象始终为null?

时间:2018-12-30 19:48:54

标签: spring-boot spring-data

我有以下要插入到数据库中的请求正文:

{
   "vin": "1HGCR2F3XFA027534",
   "latitude": 41.803194,
   "longitude": -88.144406,
   "timestamp": "2017-05-25T17:31:25.268Z",
   "fuelVolume": 1.5,
   "speed": 85,
   "engineHp": 240,
   "checkEngineLightOn": false,
   "engineCoolantLow": true,
   "cruiseControlOn": true,
   "engineRpm": 6300,

   "tires": {
       "frontLeft": 34,
       "frontRight": 36,
       "rearLeft": 29,
       "rearRight": 34
   }
 }

我无法插入它,因为Tires对象始终映射为null。我认为json对象无法映射到Tires对象。

以下是摘要

在控制器中:

@RequestMapping(method = RequestMethod.POST, value = "/readings")
public void readVehicleStatus(@RequestBody VehicleStatus vehicleStatus){
    vehicleStatusService.readVehicleStatus(vehicleStatus);
}

VehicleStatus.java

@Entity
public class VehicleStatus {

    @Id
    private String vin;

    private Double latitude;
    private Double longitude;

    private Double fuelVolume;
    private int speed;
    private int engineHp;
    private int engineRpm;

    private boolean checkEngineLightOn;
    private boolean engineCoolantLow;
    private boolean cruiseControlOn;

    @Embeded
    private Tires tires;

    /* getters and setters created */

}

Tires.java

   @Embeddable
   public class Tires {

       @Id
       @GeneratedValue(strategy= GenerationType.IDENTITY)
       int id;

       private int frontLeft;
       private int frontRight;
       private int rearLeft;
       private int rearRight;

       /* getters and setters included */
    }

1 个答案:

答案 0 :(得分:1)

在您的DOMContentLoaded中,Tires json属性被命名为VehicleStatus,而在json中,命名为vehicleTire,这就是为什么总是为null的原因,因此您应该添加tires。 / p>

这是完整的代码:

@JsonProperty(value="tires")

TiresRepository:

@Entity
public class VehicleStatus implements Serializable{

    @Id
    private String vin;

    private Double latitude;
    private Double longitude;

    private Double fuelVolume;
    private int speed;
    private int engineHp;
    private int engineRpm;

    private boolean checkEngineLightOn;
    private boolean engineCoolantLow;
    private boolean cruiseControlOn;

    @OneToOne(mappedBy = "vehicleStatus",cascade=CascadeType.ALL)
    @JoinColumn(name = "id")
    @JsonProperty(value="tires") // here is the key :)
    private Tires vehicleTire;

  // Getters && Setters
}

@Entity
public class Tires implements Serializable{

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    Integer id;

    private Integer frontLeft; // NB try always to use wrapper type rather than primitive in jpa
    private Integer frontRight;
    private Integer rearLeft;
    private Integer rearRight;

    @OneToOne
    private VehicleStatus vehicleStatus;

    // Getters && Setters
}

VehicleStatusRepository:

public interface TiresRepository extends JpaRepository<Tires, Integer>{

}

控制器示例:

public interface VehicleStatusRepository extends JpaRepository<VehicleStatus, String>{

}

主类:

@RestController
@RequestMapping
class MyController {

    @Autowired
    private VehicleStatusRepository vehicleStatusRepository;

    @RequestMapping(method = RequestMethod.POST, value = "/readings")
    public void readVehicleStatus(@RequestBody VehicleStatus vehicleStatus){
        vehicleStatusRepository.saveAndFlush(vehicleStatus);
    }
}

注意:不要忘记级联所有,否则轮胎对象将不会保存在数据库中,并且会出现以下异常:

@SpringBootApplication
@EnableAutoConfiguration
public class SpringStackOverflowSolutionApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringStackOverflowSolutionApplication.class, args);
    }

}