-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: multiple insert statement
PostPosted: Fri Mar 23, 2007 2:47 am 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
i have 2 tables, thread and attachment. the structure of the tables are as follows:

thread
id (PK) auto-increment
msgText
submitDate
username

attachment
id(PK)
threadId (FK to thread 'id' column)
file
filename


I need to do my insert for these 2 tables. im stuck because i dont know how to insert into the 'attachment' table. The threadId is tagged to 'thread' id column. In hibernate how should i do it?

please advice. thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 7:54 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, just associate an attachment with a thread and you're done... Please show your javabeans and your mappings too.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 8:37 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi fangEve,

it looks like it's a typical parent-child (one-to-many) relationship. To insert into both tables do the following:

1 / - Java code: add an instance of your Thread POJO class into
Attachment POJO class (with setter /getter )

2/ - Java code: add a Collection of Attachments into your Thread POJO (e.g. a Set)

3/ - add a convenience method to add an Attachment into a Thread (into your Thread POJO). E.g :

Code:
   public void addAttachment(Attachment at{
      at.setThread(this);
      if (attachments == null) {
         attachments = new HashSet();
      }
      attachments.add(at);
   }


4/ - Mapping: into your Thread.hbm.xml add the one-to-many relationship. E.g

Code:
      <set name="attachments" cascade="save-update">
         <key column="THREAD_ID" not-null="true"/>
         <one-to-many class="bla.bla.Attachment" />
      </set>


5/ - Mapping: into your Atachment.hbm.xml add the many-to-one relationship. E.g:

Code:
      <many-to-one name="customer" foreign-key="CUSTOMER_ID" not-null="true"/>


6/ - Don't forget to use id generator class="native" for your 'auto_increment' stuff.

That should work fine.

Don't forget to rate

Chucky
----------------

thread
id (PK) auto-increment
msgText
submitDate
username

attachment
id(PK)
threadId (FK to thread 'id' column)
file
filename


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 8:41 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Sorry the last mapping (5/) in your Attachment.hbm.xml should be :

Code:
      <many-to-one name="thread" foreign-key="THREAD_ID" not-null="true"/>


Don't forget to rate.
Chucky
--------


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 4:15 am 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
im having the following error:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com..MessageAttachment.reply

what have i done wrongly? please advice, thanks.



Code:
[b]my mapping for attachment:[/b]
<hibernate-mapping>
    <class name="com.Attachment" table="ATTACHMENT">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>      
        <many-to-one name="reply"
        class="com.thread"
        foreign-key="THREAD_ID" not-null="true"/>
      <property name="filename" type="java.lang.String" column="FILENAME"/>
        <property name="file" type="binary" column="ATTACHMENT"/>       
           </class>       
</hibernate-mapping>


Code:
[b]my mapping for thread:[/b]
<hibernate-mapping>
    <class name="com.Reply" table="COLLAB_MESSAGE">
        <id name="id" column="ID">
            <generator class="increment"/>
        </id>      
       </many-to-one>
       <set name="file" cascade="save-update">
           <key column="MESSAGE_ID" not-null="false"/>
           <one-to-many class="com.MessageAttachment"/>
       </set>
        <property name="content" type="java.lang.String" column="MSG_TEXT"/>
        <property name="submitUserId" type="java.lang.Integer" column="SUBMIT_USER_ID"/>     
        <property name="creationDate" type="java.util.Date" column="CREATED_DATE"/>
    </class>       
</hibernate-mapping>



Code:
[b]my java action class code:[/b]
MessageForm oForm =(MessageForm) form;
        Reply oReply= new Reply();
        Messenger msgr= new Messenger();
        MessageAttachment oMsgAttachment =  new MessageAttachment();
        oReply.setContent(oForm.getContent());
        oReply.setSubmitUserId(oForm.getSubmitUserId());
        oReply.setCreationDate(dtNowDate);
        oMsgAttachment.setFile(oForm.getFile().getFileData());
        oMsgAttachment.setFilename(oForm.getFile().getFileName());       
        msgr.addThreadMessage(oReply,oMsgAttachment);


Last edited by fangEve on Sun Mar 25, 2007 9:28 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 8:03 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
I guess you didn't provide all your mappings, namely the MessageAttachment one.

In this class, there seems to be a not-null relationship to Reply. I guess you didn't set it.

One other bizarre thing, you mapped a set to something named "file". Most of the time, collection mappings are plural.
Quote:
<set name="file" cascade="save-update">
<key column="MESSAGE_ID" not-null="false"/>
<one-to-many class="com.leadingside.core.common.pojo.MessageAttachment"/>
</set>

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 10:09 am 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
batmat wrote:
I guess you didn't provide all your mappings, namely the MessageAttachment one.

The MessageAttachment is actually mean Attachment. It's my typo

batmat wrote:
In this class, there seems to be a not-null relationship to Reply. I guess you didn't set it.

How do i do the mapping? can give an example? Im new to hibernate.


batmat wrote:
One other bizarre thing, you mapped a set to something named "file". Most of the time, collection mappings are plural.
Quote:
<set name="file" cascade="save-update">
<key column="MESSAGE_ID" not-null="false"/>
<one-to-many class="com.leadingside.core.common.pojo.MessageAttachment"/>
</set>

okie, noted. thanks. i will change it


thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 8:43 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi fangEve,

before saving your thread message, you must set the parent of the attachment (it's the thread that your saving). Because you specified the Attachment mapping that the parent (the Thread) is not null, so you must set it explicitly. I don't know how your "Messenger.addThreadMessage(oReply,oMsgAttachment);" looks like but you should have something like:
Code:
addThreadMessage(oReply,oMsgAttachment) {
  oMsgAttachment.setReply(oReply);
  ...
  session.saveOrUpdate(oReply);
}



chucky
-----------------------
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 9:10 pm 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
hi, chucky

thanks. i manage to get it work. but now i ned to select the records out. how am i going to do the query? i have no idea on how to query, to get the child and parent

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 4:59 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi fangEve,
you have different possibilities to make queries:
1/ - via Criteria:

Code:
  Criteria criteria = session.createCriteria(YourThread.class);
  List threads = criteria.list(); // you get a list of your Thread
  for (int i = 0; i < threads.size(); i++) {
      YourThread t = (YourThread)threads.get(i); // you get a thread
      Set attachments = t.getAttachments(); // you get the associated attachments
  }


2/ - via HQL:
Code:
  Query q = session.createQuery("from bla.bla.YourThread");
  List threads = q.list(); // you get a list of your Thread
  for (int i = 0; i < threads.size(); i++) {
      YourThread t = (YourThread)threads.get(i); // you get a thread
      Set attachments = t.getAttachments(); // you get the associated attachments
  }


3/- via native SQL (check the hibernate documention)

Chucky - DON'T FORGET TO RATE if that helps...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 7:39 am 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
i have another problem, is related to the 2 tables asked previously.

thread
id (PK) auto-increment
msgText
submitDate
userid

user
id
userid
username


admin
id
userid username


i need to retrieve the username from either user or admin table. In the thread table, the userid, will appear either in user of the admin table. so how do i do the query?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 27, 2007 8:46 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi fangEve,
your table structure is not clear. userId refers to what? The id of the User table? If yes, why does the User table have id AND userId columns?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:26 am 
Beginner
Beginner

Joined: Sat Mar 03, 2007 5:32 am
Posts: 20
[quote="chucky"]2/ - via HQL:
Code:
  Query q = session.createQuery("from bla.bla.YourThread");
  List threads = q.list(); // you get a list of your Thread
  for (int i = 0; i < threads.size(); i++) {
      YourThread t = (YourThread)threads.get(i); // you get a thread
      Set attachments = t.getAttachments(); // you get the associated attachments
  }

[quote]

how am i going to dislay the attachments? im using struts. I need to display the attachment id, and filename. thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 4:16 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi fangEve,

Once you get your Set of attachments, all you need is to iterate through it.
You get each time an Attachment object, then you retrieve the properties you need:
So to continue the example:
Code:
  Query q = session.createQuery("from bla.bla.YourThread");
  List threads = q.list(); // you get a list of your Thread
  for (int i = 0; i < threads.size(); i++) {
      YourThread t = (YourThread)threads.get(i); // you get a thread
      Set attachments = t.getAttachments(); // you get the associated attachments
     
[b]      Iterator it = attachments.iterator();
      while (it.hasNext()) {
         Attachment at = (Attachment) it.next();
         long id = at.getId();
         String fn = at.getFileName();
         ...

      }[/b]
     
  }


With Struts, you could use the <logic:iterate> tag to iterate through your Set of attachments.
see Struts documentation to see how to use it:
http://struts.apache.org/1.3.8/struts-t ... ml#iterate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 4:39 am 
Newbie

Joined: Tue Mar 20, 2007 5:13 am
Posts: 9
1 question related to this issue ....

if you have a set <one-to-many> at parent's table
and <many-to-one> at child's table.

If you want to set a new parent for a child ... do u need to...
a
Code:
child.setParent(newParent);


or

b
Code:
Parent oldParent = child.getParent();
oldParent.removeChild(child);
newParent.addChild(child);
child.setParent(newParent);



in summary ... do u need to set each parent and child manually or hibernate would taking care of it??

This is if cascade="all"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.