【问题标题】:Embeded object is always null?嵌入对象始终为空?
【发布时间】:2019-05-27 14:23:28
【问题描述】:

我有以下请求正文,我正在尝试将其插入数据库:

{
   "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
   }
 }

我无法插入它,因为轮胎对象总是映射为空。我不认为 json 对象能够映射到轮胎对象。

这里是sn-ps

在控制器中:

@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 */

}

轮胎.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 */
    }

【问题讨论】:

    标签: spring-boot spring-data


    【解决方案1】:

    在你的VehicleStatus轮胎json属性被命名为vehicleTire但在json被命名为tires,这就是为什么总是空,所以你应该添加@JsonProperty(value="tires")

    这里是完整的代码:

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

    轮胎仓库:

    public interface TiresRepository extends JpaRepository<Tires, Integer>{
    
    }
    

    VehicleStatusRepository:

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

    注意:不要忘记 cascadeAll,否则轮胎对象将不会保存在数据库中,您将得到以下异常:

    org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : package.VehicleStatus.vehicleTire -> package.Tires
    

    【讨论】:

    • 您将如何使用@Embeded 和@Embeddable 来做到这一点?
    • 我认为你不需要使用@Embeded@Embeddable
    • 是的,它奏效了。我也错过了@JsonProperty,就像你说的那样,这是关键,谢谢:)
    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2019-07-28
    • 2019-03-06
    • 2014-01-17
    • 2012-09-16
    • 2015-12-27
    相关资源
    最近更新 更多