gpt4 book ai didi

jpa - 如何在 JPA2 和 Spring Data JPA 中处理具有关系的复合键?

转载 作者:行者123 更新时间:2023-12-04 02:11:47 26 4
gpt4 key购买 nike

我在处理 mysql 表的映射对象关系时遇到问题。我有如下所示的 2 个表:

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

ApkInfo
--------
id PK
packageName
appName
deviceId FK

然后这是我的类(class):

@Entity
@Table(name="Device")
public class Device implements Serializable {

@Column
@Id
private String deviceId;

@Column
private String deviceName;

//getters and setters

}


@Entity
@Table(name="ApkInfos")
public class ApkInfo implements Serializable {

@Column
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

@Column
@Id
private String packageName;

@Column
private String appName;

@Column
@Temporal(TemporalType.TIMSTAMP)
private Date installDate;

@ManyToOne
@JoinColumn(name="deviceId" referencedColumnName="deviceId")
private Device device;

//getters and setters

}

这对我有用,但我想在 ApkInfos 表中使用复合键 deviceIdpackageName

@Entity
@Table(name="ApkInfos")
public class ApkInfo implements Serializable {

@Colum(instable=false, updatable=false)
@Id
private String deviceId;

@Column
private String packageName;

@Column
private String appName;

@ManyToOne
@JoinColumn(name="deviceId" referencedColumnName="deviceId")
private Device device;

//getters and setters

}

但是当我尝试使用 Spring Data JPA 存储库保存实体时,出现错误:

org.springframework.dao.InvalidAccessApiUsageException: Class must not be null, nested exception is java.lang.IllegalArgumentException: Class must not be null

ApkInfo apkInfo = new ApkInfo();
apkInfo.setDeviceId("1234");
apkInfo.setPackageName("aaa");
apkInfo.setAppName("myapp");
apkInfo.setInstallDate(new Date());
apkInfo.setDevice(new Device("1234"));

repository.save(apkInfo);

并且设备的 deviceID '1234' 已经存在于 Device 表中。

最佳答案

我创建了一个单独的主键类,在 ApkInfo 类中添加了 @IdClass。现在工作正常,谢谢。稍后我将进一步了解 EmbeddedId。

我在实体类中添加了@IdClass,在 packageName 属性中添加了@Id。我还为一对多列插入、更新 false。

@Entity
@Table(name="ApkInfos")
@IdClass(ApkInfo.class)
public class ApkInfo implements Serializable {

@Column @Id private String deviceId;
@Column @Id private String packageName;

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


//getters and setters missing
}

主键类只有 setter 并重写 equals 和 hasCode 方法。

public class ApkInfo implements Serializable {

private String deviceId;
private String packageName;


public ApkInfo(){}
public ApkInfo (String deviceId, String packageName){
this.deviceId = deviceId;
this.packageName = packageName;
}

public String getDeviceId(){
return this.deviceId;
}

public String getPackageName(){
return this.packageName;
}

@Override
public boolean equals(Object obj){
return (obj!=null &&
obj instanceof ApkInfoPk &&
deviceId.equals(((ApkInfoPk)obj).getDeviceId()) &&
packageNames.equals(((ApkInfoPk)obj).getPackageName()) );
}

@Override
public int hashCode(){
super.hashCode();
}
}

关于jpa - 如何在 JPA2 和 Spring Data JPA 中处理具有关系的复合键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16391021/

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