gpt4 book ai didi

spring - 为复合 PK 生成的空 ID

转载 作者:行者123 更新时间:2023-12-04 00:43:13 25 4
gpt4 key购买 nike

我有以下表格 enter image description here以及以下关系表:enter image description here , 它有一个复合 PK 如下:

用户角色.java

@RooJavaBean
@RooJpaEntity(identifierType = UserRolePK.class, versionField = "", table = "UserRole", schema = "dbo")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "idApplication", "idRole", "idUserName" })
public class UserRole {
}

UserRole_Roo_DbManaged.aj

@ManyToOne
@JoinColumn(name = "IdApplication", referencedColumnName = "IdApplication", nullable = false, insertable = false, updatable = false)
private Application UserRole.idApplication;

@ManyToOne
@JoinColumn(name = "IdRole", referencedColumnName = "IdRole", nullable = false, insertable = false, updatable = false)
private Role UserRole.idRole;

@ManyToOne
@JoinColumn(name = "IdUserName", referencedColumnName = "IdUserName", nullable = false, insertable = false, updatable = false)
private Users UserRole.idUserName;

但也存在一个PK表:

@RooIdentifier(dbManaged = true)
public final class UserRolePK {}

及其标识符类(UserRolePK_Roo_Identifier.aj)

privileged aspect UserRolePK_Roo_Identifier {

declare @type: UserRolePK: @Embeddable;

@Column(name = "IdRole", nullable = false)
private Long UserRolePK.idRole;

@Column(name = "IdUserName", nullable = false, length = 16)
private String UserRolePK.idUserName;

@Column(name = "IdApplication", nullable = false)
private Long UserRolePK.idApplication;

我设置服务对象保存的方式是:

UserRole userRole= new UserRole();
userRole.setIdApplication(app);
userRole.setIdRole(invited);
userRole.setIdUserName(user);
appService.saveURole(userRole);

app之前已经设置并保存(同一交易),以及invited用户 对象。由于用户(来自具有复合 PK 的 Users 表:IdUserName,它是一个 String )定义如下,否则不起作用。

@RooJavaBean
@RooJpaEntity(versionField = "", table = "Users", schema = "dbo")
@RooDbManaged(automaticallyDelete = true)
@RooToString(excludeFields = { "quotations", "taxes", "userRoles", "idCompany", "idPreferredLanguage" })
public class Users {

@Id
//@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "IdUserName", length = 16, insertable = true, updatable = true)
private String idUserName;
}

所以,我得到的错误是:

org.springframework.orm.jpa.JpaSystemException: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.domain.UserRole; nested exception is javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.domain.UserRole

最佳答案

试试这个:

public class UserRole {
@PrePersist
private void prePersiste() {
if (getId() == null) {
UserRolePK pk = new UserRolePK();
pk.setIdApplication(getIdApplication());
pk.setIdRole(getIdRole);
pk.setIdUserName(getIdUserName());
setId(pk);
}
}
}

Roo 正在生成 UserRole 实体及其 id 嵌入类的字段,但不是一回事(UserRole.idRole 与 UserRole.id 不同。身份角色)。在您的示例中,您填写了 UserRole 字段,但没有填写 id 字段。此代码会在实体持久化之前为您完成。

祝你好运!

关于spring - 为复合 PK 生成的空 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26320983/

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