3 Replies Latest reply on Jul 12, 2006 10:57 AM by abl

    Entity Bean behaves strangely with @OneToMany

    hti-patmas_team

      Hello everybody

      We've encountered a problem with an Entiy Bean. If we make an instance of the folowing bean persistent while the collection are empty they are added to the database correctly. The retrieval of the data works also.

      But: As soon as more than one collection contains more than one element we following problem occurs: The bean-data is fetched by a LEFT OUTER JOIN query (Found out with Ethereal). This causes the collections to contain as much elements as rows are returned by the DBMS.


      Example
      Collection1: 3 entries
      Collection2: 4 entries

      JOIN causes the collections to contain 12 entries (cartesian product)
      -> This is an undesired effect.

      Relevant bean code

      @Entity(access=AccessType.PROPERTY)
      @Table(name="BibliographicData")
      public class BibliographicData implements Serializable {
      
       private int id;
      
       private List<String> abstracts = new LinkedList<String>();
       private List<String> inventors = new LinkedList<String>();
       private List<String> applicants = new LinkedList<String>();
      
      
       @OneToMany(fetch=FetchType.EAGER)
       public List<String> getAbstracts() {
       return abstracts;
       }
      
       @OneToMany(fetch=FetchType.EAGER)
       public List<String> getApplicants() {
       return applicants;
       }
      
       @OneToMany(fetch=FetchType.EAGER)
       public List<String> getInventors() {
       return inventors;
       }
      
       ....
      }
      
      
      


      Workaround
      Removal of @OneToMany causes JBoss to store the collections in serialized form. The problem disappears.

      Question
      Is there a clever way to store the collections in separate tables without separating them into individual entity beans?

      Thanks for your help!
      Regrads, Mike
      JBoss Configuration
      JBoss Version: 4.0.3 SP1
      Sun JRE5 Build 1.5.0_06-b05
      Debian Linux (Sarge)
      MySQL 5.0.22

      Changes in mysql-ds.xml
      <datasources>
       <local-tx-datasource>
       <jndi-name>MySqlDS</jndi-name>
       <connection-url>jdbc:mysql://localhost:3306/jbossdb</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>jboss</user-name>
       <password>insertpasswdhere</password>
       <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
       <!-- sql to call when connection is created
       <new-connection-sql>some arbitrary sql</new-connection-sql>
       -->
       <!-- sql to call on an existing pooled connection when it is obtained from pool
       <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
       -->
      
       <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
       <metadata>
       <type-mapping>mySQL</type-mapping>
       </metadata>
       </local-tx-datasource>
      </datasources>