2

我有 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时,我没有获取设备数据;

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

4

1 回答 1

10

按如下方式更改映射。

@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);
于 2013-05-28T12:43:34.137 回答