JPA: Foreign key that is also a primary key mapping

前端 未结 4 554
Happy的楠姐
Happy的楠姐 2020-12-09 10:05

I have been trying to solve this for whole day but no luck! Also i tried to read most of the tutorials on the net but as you all know they all are full of useless examples t

4条回答
  •  佛祖请我去吃肉
    2020-12-09 10:29

    I know this is very old qs, but for completeness of your case you can just have (jpa 2.0)

    @Entity
    @Data
    public class Vehicle implements Serializable{
       @Id
       @GeneratedValue
       private long vehicleId;
       .. //other props
    }
    
    @Entity
    @Data
    public class VehicleExtras implements Serializable{
             @Id
             @OneToOne (cascade = CASCADE.ALL)
             @MapsId
             @JoinColumn(name ="vehicleId")
             private Vehicle vehicle;
    
             @Column  
             private boolean allowSmoke; 
             ..// other props.
    }
    

    should share same pk/fk for VehicleExtra table

提交回复
热议问题