为复合PK生成的null id

时间:2014-10-12 01:33:35

标签: spring jpa jpa-2.0 spring-data spring-roo

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

UserRole.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 已设置并保存(同一交易),以及 邀请 用户 对象。 由于user(来自Users表的复合PK: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

2 个答案:

答案 0 :(得分:6)

试试这个:

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.idRole不同)。在您的示例中,您填写UserRole字段,但不填写id字段。此代码在实体持久化之前为您提供。

祝你好运!

答案 1 :(得分:0)

在我的情况下,如果以下示例尝试保留在数据库中,则会引发上面提到的类似异常:

EntityExample e = new EntityExample();
repositoryExample.save(e);
//throw ex

这是由于缺少ID字段值而引起的,需要将其设置为以下内容:

EntityExample e = new EntityExample();
e.setId(new EmbeddedIdExample(1, 2, 3));
repositoryExample.save(e);