3 Replies Latest reply on Jan 6, 2006 5:12 AM by pieterjan

    Stop automatic persist of parents children?

    pieterjan

      Hi,

      I came upon this problem when looking at the hibernate queries that JBoss AS executes for bi-directional relations (i.e. parent-child).

      The problem is that when a method of a session bean is called and in that method a parent entity bean is used, then after finishing the method the server will execute a ton of select statements for the children.
      My guess, to merge or update the children.

      But seeing as I only access properties, I would like a way to stop this automatic update of the children.

      If anyone knows a way, I'm all ears.


      The annotations for parent and children I use now look as follows:


      
      // in the parent entity bean
      
       @OneToMany(targetEntity=ChildBean.class,
       mappedBy="parent",
       cascade=CascadeType.ALL,
       fetch=FetchType.LAZY)
       private List<ChildBean> children;
      
      
      // int the child entity bean
      
       @ManyToOne(targetEntity=ParentBean.class, fetch=FetchType.LAZY)
       @JoinColumn(name="parent_id", nullable = false)
       private ParentBean parent;
      
      


        • 1. Re: Stop automatic persist of parents children?
          pieterjan


          Interesting.

          I found the following link http://e-docs.bea.com/workshop/docs81/doc/en/core/index.html at the bea website.
          Where in the section 'Developing Enterprise JavaBean' > 'Developing Entity Beans' > 'Accelerating Entity Bean Data Access' they give a few pointers on how to make entity beans that are read only.

          I wonder if I could do the same in JBoss and if this would prevent the server from trying to update each entity bean?

          • 2. Re: Stop automatic persist of parents children?
            pieterjan

            I found out that the simple parent-child example I gave, does not produce the selection statements I was talking about. Instead a parent-child-grandchild relation does produce the problem.


            I used the following classes to test (the getters and setters are removed here) :

            
            // Parent
            
            @Entity(access = AccessType.FIELD)
            @Table(name="t_tests", schema="grid")
            @SequenceGenerator(name="TestBeanSequence", sequenceName="grid.s_tests")
            public class TestBean implements Serializable {
            
             @Id(generate=GeneratorType.SEQUENCE, generator="TestBeanSequence")
             private Integer id;
            
             @Column(length=255)
             private String someText;
            
             @Basic(temporalType = TemporalType.TIMESTAMP)
             private Date someDate;
            
             private Boolean someBoolean;
            
             @OneToMany(targetEntity=TestChildBean.class,
             mappedBy="test",
             cascade=CascadeType.ALL,
             fetch=FetchType.LAZY)
             private List<TestChildBean> testChildren;
            }
            
            
            // Child
            
            @Entity(access = AccessType.FIELD)
            @Table(name="t_testchilds", schema="grid")
            @SequenceGenerator(name="TestChildBeanSequence", sequenceName="grid.s_testchilds")
            public class TestChildBean {
            
             @Id(generate=GeneratorType.SEQUENCE, generator="TestChildBeanSequence")
             private Integer id;
            
             @Column(length=20, nullable=false)
             private String name;
            
             @ManyToOne(targetEntity=TestBean.class, fetch=FetchType.LAZY)
             @JoinColumn(name="test_id", nullable = false)
             private TestBean test;
            
             @OneToMany(targetEntity=TestGrandChildBean.class,
             mappedBy="testChild",
             cascade=CascadeType.ALL,
             fetch=FetchType.LAZY)
             private List<TestGrandChildBean> testGrandChildren;
            }
            
            // Grandchild
            
            @Entity(access = AccessType.FIELD)
            @Table(name="t_testgrandchilds", schema="grid")
            @SequenceGenerator(name="TestGrandChildBeanSequence", sequenceName="grid.s_testgrandchilds")
            public class TestGrandChildBean {
            
             @Id(generate=GeneratorType.SEQUENCE, generator="TestGrandChildBeanSequence")
             private Integer id;
            
             @Column(length=20, nullable=false)
             private String name;
            
             @ManyToOne(targetEntity=TestChildBean.class, fetch=FetchType.LAZY)
             @JoinColumn(name="testchild_id", nullable = false)
             private TestChildBean testChild;
            }
            


            • 3. Re: Stop automatic persist of parents children?
              pieterjan

              Solved it!

              Apparently it had nothing to do with read-only beans but with the way JBoss 4.0.2 checks for dirty entity beans.
              Upgrading to 4.0.3SP1 solved the problem.

              BTW, the performance increase is HUGE.