使用复合键启动 JPA 存储库 saveAndFlush
Boot JPA Repository saveAndFlush with composite key
我目前正在尝试使用 Credential 对象的 JPARepository 接口执行持久化,该对象将复合键作为主键。
回购是:
@Transactional
public interface CredentialRepository extends JpaRepository<Credential,CredentialPK> {}
凭证PK为:
@Embeddable
public class CredentialPK implements Serializable {
@Column(name = "CREDENTIAL_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer credentialID;
@Column(name = "CREDENTIAL_TYPE_ID")
private String credentialTypeID;
public CredentialPK() {
}
//getters and setters;
}`
凭证:
@Entity
@Table(name = "TBL_CREDENTIALS")
public class Credential {
@EmbeddedId
private CredentialPK credentialPK;
//getters and setters
}
在服务 class 中,我正在尝试使用应用程序提供的凭据类型执行保存操作。假设为了示例的缘故,凭证类型是 'temporary'。
创建和持久化凭证的代码如下:
Credential credential = new Credential();
CredentialPK pk = new CredentialPK();
pk.setCredentialTypeID("temporary");
credential.setCredentialPK(pk);
Credential temporaryCredential = credentialRepository.saveAndFlush(credential);
temporaryCredential.getCredentialPK().getCredentialID(); // returns null, even when repository is flushed
`
我要求 temporaryCredential 的 credentialID 使用新插入的凭证的 ID 填充。但是,我得到的 credentialID 为空。我已经尝试将 CrudRepository api 和 JpaRepository api 用于存储库实现,同时 save 和 saveAndFlush 方法。如果凭据的主键不是复合的,这将完美运行,但由于遗留代码,我无法进行此更改。
有什么建议吗?
来自另一个答案:
I have been hovering over all the possible links on World Wide Websites and trying to find why you cannot use @GeneratedValue with @EmbeddedId or @IdClass (i.e. composite PKs). The reason is that you just CANNOT.
Composite PKs are ASSIGNMENT-based not GENERATION-based. Therefore, any @GeneratedValue stuff are not supposed to work with them. I am also having problem in my project and I think there is no other way
If you know that your @GeneratedValue ID is always unique in context of your domain (for example, your database) you don't need to use composite PK and have some internal checks to determine the uniqueness of your records
所以,就像楼主说的,如果你有一个@GeneratedValue
,你不需要复合PK,产生的值是唯一的。 creditialId
和 credentialTypeId
的任何组合都不会是唯一的。这基本上意味着您需要重新考虑您的设计,恕我直言。
我目前正在尝试使用 Credential 对象的 JPARepository 接口执行持久化,该对象将复合键作为主键。 回购是:
@Transactional
public interface CredentialRepository extends JpaRepository<Credential,CredentialPK> {}
凭证PK为:
@Embeddable
public class CredentialPK implements Serializable {
@Column(name = "CREDENTIAL_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer credentialID;
@Column(name = "CREDENTIAL_TYPE_ID")
private String credentialTypeID;
public CredentialPK() {
}
//getters and setters;
}`
凭证:
@Entity
@Table(name = "TBL_CREDENTIALS")
public class Credential {
@EmbeddedId
private CredentialPK credentialPK;
//getters and setters
}
在服务 class 中,我正在尝试使用应用程序提供的凭据类型执行保存操作。假设为了示例的缘故,凭证类型是 'temporary'。 创建和持久化凭证的代码如下:
Credential credential = new Credential();
CredentialPK pk = new CredentialPK();
pk.setCredentialTypeID("temporary");
credential.setCredentialPK(pk);
Credential temporaryCredential = credentialRepository.saveAndFlush(credential);
temporaryCredential.getCredentialPK().getCredentialID(); // returns null, even when repository is flushed
`
我要求 temporaryCredential 的 credentialID 使用新插入的凭证的 ID 填充。但是,我得到的 credentialID 为空。我已经尝试将 CrudRepository api 和 JpaRepository api 用于存储库实现,同时 save 和 saveAndFlush 方法。如果凭据的主键不是复合的,这将完美运行,但由于遗留代码,我无法进行此更改。
有什么建议吗?
来自另一个答案:
I have been hovering over all the possible links on World Wide Websites and trying to find why you cannot use @GeneratedValue with @EmbeddedId or @IdClass (i.e. composite PKs). The reason is that you just CANNOT.
Composite PKs are ASSIGNMENT-based not GENERATION-based. Therefore, any @GeneratedValue stuff are not supposed to work with them. I am also having problem in my project and I think there is no other way
If you know that your @GeneratedValue ID is always unique in context of your domain (for example, your database) you don't need to use composite PK and have some internal checks to determine the uniqueness of your records
所以,就像楼主说的,如果你有一个@GeneratedValue
,你不需要复合PK,产生的值是唯一的。 creditialId
和 credentialTypeId
的任何组合都不会是唯一的。这基本上意味着您需要重新考虑您的设计,恕我直言。