gpt4 book ai didi

jpa - 使用 Spring Data JPA 映射和获取具有复合键的实体

转载 作者:行者123 更新时间:2023-12-02 11:22:38 25 4
gpt4 key购买 nike

我有 Spring MVC 和 Data JPA。我正在尝试制作我拥有的映射表。结构如下:

Device
--------------
PK deviceId
deviceName


Setting
--------------
PK deviceId
PK packageName
PK name
value

我有这些表的类:

@Entity
public class DeviceSetting implements Serializable {
@EmbeddedId
private String deviceId
private String deviceName;

@ManyToOne
@JoinColumn(name="deviceId", referencedColumnName="deviceId", insertable=false, updatable=false)
private Device device;

//Setters and Getters
}

@Embeddable
public class DeviceSettingPk implements Serializable {

private String deviceId;
private String packageName;
private String name;

public DeviceSettingPk(){}
public DeviceSettingPk(String deviceId, String packageName, String name) {
super();
this.deviceId = deviceId;
this.packageName = packageName;
this.name = name;
}

//Setters and Getters
}

@Entity
public class Device implements Serializable {

private static final long serialVersionUID = 1L;

@NotEmpty
@Id
@Column(name="deviceId")
private String deviceId;

private String deviceName;

//Getters and Setters
}

但是当我放置设备并且设置数据具有相同的deviceId并使用repository.findOne(deviceId)查询DeviceSetting时,我没有获取设备数据

我还需要做什么才能获取设备数据?任何意见将是有益的。谢谢。

最佳答案

按如下方式更改映射。

@Entity
public class DeviceSetting implements Serializable {
@EmbeddedId
private DeviceSettingPk deviceSettingId;


@ManyToOne
@JoinColumn(name="deviceId", referencedColumnName="deviceId", insertable=false, updatable=false)
private Device device;

//Setters and Getters
}

然后使用下面的代码进行查找。

DeviceSettingPk id = new DeviceSettingPk(deviceId, packageName, name);

// use the deviceSettingRespository to lookup
repository.findOne(id);

关于jpa - 使用 Spring Data JPA 映射和获取具有复合键的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com