1

Hibernate を使用してエンティティのアクセス許可を保持するのに問題があります。属性 Group (Foreign key) を持つ多くの Permission オブジェクトを保存する必要がありますが、同時に Permission を保存していて、Group を保存しているため、設定する Group ID (主キー) がありません。関連付けを行う権限オブジェクト。

私の関係:

@Entity
@BatchSize(size = 10)
public class Group implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "grupo")
    @JsonIgnore
    private List<Permissao> permissoes;
}

@Entity
@BatchSize(size = 10)
public class Permission implements Serializable {

    @ManyToOne
    private Grupo grupo;
}

スタック:

SEVERE: Servlet.service() for servlet [hemisphere-web] in context with path 
[/hemisphere-web] threw exception [Request processing failed; nested exception is 
org.springframework.dao.InvalidDataAccessApiUsageException: 
org.hibernate.TransientPropertyValueException: object references an unsaved transient 
instance - save the transient instance before flushing: 
net.pontoall.hemisphere.core.model.Permission.grupo -> 
net.pontoall.hemisphere.core.model.Grupo; nested exception is 
java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object 
references an unsaved transient instance - save the transient instance before flushing: 
net.pontoall.hemisphere.core.model.Permissao.grupo -> 
net.pontoall.hemisphere.core.model.Grupo] with root cause
org.hibernate.TransientPropertyValueException: object references an unsaved transient 
instance - save the transient instance before flushing: 
net.pontoall.hemisphere.core.model.Permissao.grupo ->        net.pontoall.hemisphere.core.model.Grupo

誰かヒントをくれませんか?

4

2 に答える 2

1

まず第一に、JPA/Hibernate を使用しているときは、オブジェクトを使用しています。グループ ID を許可オブジェクトに設定する必要はなく、グループ インスタンスに渡すだけです。

マッピングによると、グループのアクセス許可リストにカスケードがあるため、グループを保存すると正常に動作するはずです。最初にグループを保存せずに権限を保存しようとすると、おそらく TransientObjectException が発生します。

于 2013-05-16T13:14:21.967 に答える
1

または、パーミッションからグループへのカスケードを使用することもできます。そのようにして、新しいグループに属する新しいパーミッションを保存すると、グループも永続化されます。

@ManyToOne(cascade=CascadeType.PERSIST)
private Group group;
于 2013-05-16T15:08:33.530 に答える