Perform the following mapping with JPA:
{code:java} public class Papel{ .... @OneToMany(mappedBy = "id.papel", cascade = { CascadeType.ALL } , orphanRemoval = true) private List<PermissaoAtribuida> permissoesAtribuidas = new ArrayList<>(); .... } public class Permissao { .... @Id @Getter @Setter @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "PK", insertable = false, updatable = false) private Integer id; .... } public class PermissaoAtribuida{ .... @EmbeddedId private Id id; @Embeddable @EqualsAndHashCode(of = { "permissao", "papel" } ) public static class Id { @ManyToOne(optional = false, fetch = FetchType.LAZY) @JoinColumn(name = "FK_PERMISSAO", nullable = false) private Permissao permissao;
@ManyToOne(optional = false, fetch = FetchType.LAZY) @JoinColumn(name = "FK_PAPEL", nullable = false) private Papel papel; } .... }
{code} And invoke the following code snippet, whereas papelServico is an EJB abstracting the DAO layer:
{code:java} Papel papel = new Papel(); papel.setNome("Insert"); papelServico.insert(papel); Permissao permissao = ... List<PermissaoAtribuida> l = new ArrayList<>(); l.add(new PermissaoAtribuida(permissao, papel)); papel.setPermissoesAtribuidas(l); papel.setNome("Update"); papelServico.update(papel); {code}
This problem only occurs if the permissaoAtribuida properties of class Papel are mapped with cascade = {CascadeType.ALL}
When executing the update of a record invoking getEntityManager().merge(obj) the application goes into infinite loop. This behavior only occurs when there is a OneToMany bidirectional mapping of the entity Papel -> PermissaoAtribuida. If the same code runs outside of the Wildfly server context its execution occurs normally. Apparently the call getEntityManager.().merge(obj) attempts infinite selects to sync the object with transaction context before doing the merge.
Attach the source code of my test classes. |
|