Uploaded image for project: 'WildFly'
  1. WildFly
  2. WFLY-11793

JPA EntityManager merge infinite loop - java.lang.StackOverflowError

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Critical Critical
    • None
    • 15.0.1.Final
    • JPA / Hibernate
    • None
    • Hide

      Perform the following mapping with JPA:

      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;
      	}
      	....
      }
      

      And invoke the following code snippet, whereas papelServico is an EJB abstracting the DAO layer:

      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);
      

      This problem only occurs if the permissaoAtribuida properties of class Papel are mapped with cascade =

      {CascadeType.ALL}

      .

      Show
      Perform the following mapping with JPA: 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; } .... } And invoke the following code snippet, whereas papelServico is an EJB abstracting the DAO layer: 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); This problem only occurs if the permissaoAtribuida properties of class Papel are mapped with cascade = {CascadeType.ALL} .

      Dear,

      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.

        1. jstack.txt
          99 kB
        2. server+querys.log
          756 kB
        3. server-1.log
          411 kB
        4. teste.zip
          27 kB

            smarlow1@redhat.com Scott Marlow
            brunomaioli_jira Bruno Maioli Martins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: