-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Email application: use <join> or <one-to-many>?
PostPosted: Wed Dec 21, 2005 5:12 am 
Newbie

Joined: Wed Dec 14, 2005 10:47 pm
Posts: 11
Hibernate version: 3.1

Name and version of the database you are using: Oracle 10g

I am trying to convert an email application from EJB to SPRING/HIBERNATE. In the database, I have 2 tables 'EMAIL' and 'ATTACHMENT'. I have 2 Java objects with the same name as the database tables. Problem comes in when trying to store their relationship. Each email has its own set of attachments (or none), so the table schema is described as follows:

Table schema:
CREATE TABLE EMAIL (
EMAIL_ID NUMBER(16) NOT NULL,
FROM_ID VARCHAR2(255) NOT NULL,
...
PRIMARY KEY ("EMAIL_ID")
);

CREATE TABLE ATTACHMENT (
EMAIL_ID NUMBER(16) NOT NULL,
FILE_NAME VARCHAR2(255) NOT NULL,
FILE_DATA BLOB NOT NULL,
FOREIGN KEY ("EMAIL_ID") REFERENCES "EMAIL" ("EMAIL_ID") ON DELETE CASCADE,
PRIMARY KEY ("EMAIL_ID", "FILE_NAME")
);


This is what I tried to do for the mapping:

Mapping documents:
<class name="Email" table="EMAIL" lazy="false">
<id name="emailID" type="integer" column="EMAIL_ID" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">EMAIL_ID_SEQ</param>
</generator>
</id>
...
<set name="attachmentList" table="ATTACHMENT" lazy="false">
<key column="EMAIL_ID"/>
<one-to-many class="Attachment"/>
</set>
</class>

<class name="Attachment" table="ATTACHMENT" lazy="false">
<id name="fileName" column="FILE_NAME"/>
<property name="fileData" type="blob" column="FILE_DATA"/>
<class>


Once deployed in Weblogic, Hibernate complains that it is an invalid mapping. I have read the online reference documentation and it is similar to the unidirectional one-to-many association on a foreign key. I don't know why the writer says it's a very unusual case but I think it's normal for this situation to happen. Anyway, can anyone help me see if there is anything wrong with my mapping? Also, it mentioned that using a join table is better but nothing much is mentioned on how to use the <join> other than for inheritence. Is it really better to use <join> and if so, how can I use it?

Adrian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 21, 2005 8:39 am 
Newbie

Joined: Mon Jun 06, 2005 8:53 am
Posts: 16
Try this:

Code:
<class name="Email" table="EMAIL" lazy="false">
...
<set name="attachmentList" table="ATTACHMENT" inverse="true" lazy="false">
  <key column="EMAIL_ID"/>
  <one-to-many class="Attachment"/>
</set>

</class>


Code:
<class name="Attachment" table="ATTACHMENT" lazy="false">
  <composite-id name="id" class="Attachment$Id">
    <key-property name="emailId" column="EMAIL_ID" />
    <key-property name="fileName" column="FILE_NAME" />
</composite-id>

  <property name="fileData" type="blob" column="FILE_DATA"/>
<class>


You need to create in your POJO an inner class Id for the composite-id.

Code:
public class Attachment {

    /**
     * Primary key ID.
     */
    public static class Id implements Serializable {
        private String fileName;
        private int emailId;

        // getters and setters...
       // implements equals and hashCode methods
   }

   private Id id = new Id();
   private Blob fileData;

    // getters and setters...
}


Cristiane


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 2:41 am 
Newbie

Joined: Wed Dec 14, 2005 10:47 pm
Posts: 11
Thanx for the reply. I have tried the method mentioned above as well as implementing Serializable in the 'Attachment' class. It doesn't seem to complain about the mapping being invalid but now the error occurs when Hibernate is trying to get the component. I have attached the extracts of the Java class and Hibernate mapping file.

Java object
Code:
public class Email  {
  private int emailID;
  ...
  private List attachmentList;

  // setters and getters
}

public class Attachment implements Serializable  {
  private int emailID;
  private String fileName;
  private Blob fileData;

  // setters and getters
  // implementation of equals() and hashCode()
}


The Hibernate mapping is as follows:
Code:
<class name="com.email.entities.Email" table="EMAIL" lazy="false">
  <id name="emailID" type="integer" column="EMAIL_ID" unsaved-value="-1">
    <generator class="sequence">
      <param name="sequence">EMAIL_ID_SEQ</param>
    </generator>
  </id>
  ...
  <set name="attachmentList" table="ATTACHMENT" lazy="false">
    <key column="EMAIL_ID"/>
    <one-to-many class="com.email.entities.Attachment"/>
  </set>
</class>

<class name="com.email.entities.Attachment" table="ATTACHMENT" lazy="false">
  <composite-id>
    <key-property name="newsID" type="integer" column="EMAIL_ID"/>
    <key-property name="fileName" type="string" column="FILE_NAME"/>
  </composite-id>
  <property name="fileData" type="blob" column="FILE_DATA"/>
<class>


Error returned
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory'
defined in ServletContext resource [/WEB-INF/spring-config/dao-config.xml]: Initialization of bean failed;
nested exception is org.hibernate.MappingException: component class not found: com.email.entities.Attachment
org.hibernate.MappingException: component class not found: com.email.entities.Attachment
        at org.hibernate.mapping.Component.getComponentClass(Component.java:101)
        at org.hibernate.tuple.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:105)
        at org.hibernate.tuple.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:40)
   ...
Caused by: java.lang.ClassNotFoundException: com.email.entities.Attachment
        at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:198)
        at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:62)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
        at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:223)
        at weblogic.utils.classloaders.ChangeAwareClassLoader.loadClass(ChangeAwareClassLoader.java:41)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:140)
        at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:108)
        at org.hibernate.mapping.Component.getComponentClass(Component.java:98)
        ... 40 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:25 am 
Newbie

Joined: Mon Jun 06, 2005 8:53 am
Posts: 16
Is this patch correct "com.email.entities.Attachment"?

Also in your attachment mapping you are declaring newID instead of emailID

The attachmentList should be a Set instead of a List on the java class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 11:04 pm 
Newbie

Joined: Wed Dec 14, 2005 10:47 pm
Posts: 11
Oops... Sorry for the mistakes... I copied the codes from an outdated copy without knowing. I have rectified the codes and it still doesn't work. The Java classes are properly packaged but I left them out in the abstract of the codes provided here. Below are the corrected codes:

Java:
Code:
public class Email  {
  private int emailID;
  ...
  private Set attachmentList;

  // setters and getters
}

public class Attachment implements Serializable  {
  private int emailID;
  private String fileName;
  private Blob fileData;

  // setters and getters
  // implementation of equals() and hashCode()
}


Hibernate mapping
Code:
<class name="com.email.entities.Email" table="EMAIL" lazy="false">
  <id name="emailID" type="integer" column="EMAIL_ID" unsaved-value="-1">
    <generator class="sequence">
      <param name="sequence">EMAIL_ID_SEQ</param>
    </generator>
  </id>
  ...
  <set name="attachmentList" table="ATTACHMENT" lazy="false">
    <key column="EMAIL_ID"/>
    <one-to-many class="com.email.entities.Attachment"/>
  </set>
</class>

<class name="com.email.entities.Attachment" table="ATTACHMENT" lazy="false">
  <composite-id>
    <key-property name="emailID" type="integer" column="EMAIL_ID"/>
    <key-property name="fileName" type="string" column="FILE_NAME"/>
  </composite-id>
  <property name="fileData" type="blob" column="FILE_DATA"/>
<class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 2:34 pm 
Newbie

Joined: Mon Jun 06, 2005 8:53 am
Posts: 16
Are you getting the same error message?

"nested exception is org.hibernate.MappingException: component class not found: com.email.entities.Attachment
org.hibernate.MappingException: component class not found: com.email.entities.Attachment " ???


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 29, 2005 9:58 pm 
Newbie

Joined: Wed Dec 14, 2005 10:47 pm
Posts: 11
Hmm... Somehow it works after some tweeking. I think was Weblogic's cached deployment in its !wldonotdelete folder. Somehow it was loading the old copy. Anyway, it works now with some improvements to the variable data-types. Removed the usage of inner class as suggested by you, so now it's simpler.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.