我在休眠中映射属性时遇到问题。我只想将 Device(1 对多)映射到 DeviceData(多对 1),反之亦然。
我的输出应该是:
表:device=id,device(一个设备号),
表:DeviceData = id, device_id (forgein key), ...
一切都由 json2ojo 生成器生成。
@Entity(name = "Device")
@Table(name = "device_devices")
public class Device {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty
private Long id;
@JsonProperty("device")
@Column(unique = true)
private String device;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "device", cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = DeviceData.class)
private List<DeviceData> deviceData = new ArrayList<>();
...
}
@Entity(name = "DeviceData")
@Table(name = "device_data")
public class DeviceData {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonProperty
private Long id;
...
@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Device.class)
@JoinColumn(name = "device_id", referencedColumnName = "id")
private Device device;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("id")
public Long getId(){
return id;
}
@JsonIgnore
public Device getDevice(){
return device;
}
@JsonIgnore
public void setDevice(Device device){
this.device = device;
}
...
...
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
BIG阳
相关分类