我在休眠中映射属性时遇到问题。我只想将 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); }}
1 回答
BIG阳
TA贡献1859条经验 获得超6个赞
默认情况下,JPA 会尝试保留类的所有属性,但您可以使用注释@Entity忽略某些属性。@Transient在您的情况下,如果您不想additionalProperties在两个类中都保留字段,则应将它们标记为@Transient:
@JsonIgnore
@Transient
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
添加回答
举报
0/150
提交
取消