-->
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.  [ 12 posts ] 
Author Message
 Post subject: How to prevent insert complicate object to author table?
PostPosted: Sat Dec 20, 2003 7:51 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi everyone:

I want to write a BBS using Hibernate.I have two tables author and article.The accociation of author and article are 1:N. I want to implement this.When a user post a article, his name will be inserted into author table and the comment of article will be inserted into article table. When the user post topic next time his name will Not be inserted into author table again. But his article will be inserted into article table normally. The author and his article will be accociated remain. I means that I can get all the articles when I use a author's ID. I try to change the code to
Code:
<hibernate-mapping>
   <class name="lyo.hotmail.htest.Parent">
      <id name="id" type="int" unsaved-value="0">
        <generator class="identity"/>
     </id>
     <property name="name" unique="true"/>
     <set name="children" lazy="true" inverse="true" cascade="all">
        <key  column="parent_id"/>
       <one-to-many class="lyo.hotmail.htest.Child"/>
     </set>
   </class>
   <class name="lyo.hotmail.htest.Child">
      <id name="id" type="int" unsaved-value="any">
        <generator class="identity"/>
     </id>
     <property name="name"/>
     <many-to-one  name="parent" column="parent_id" not-null="true"/>
   </class>
</hibernate-mapping>


But when the author post the second article his name will be inserted into author table again! The author name can't complicate in author table.
How to fix this?

Thks in advanced!

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Simple: Load the Author first from the DB, if it does not allready exist create a new one and save it. Then add the Article to the collection.


Top
 Profile  
 
 Post subject: Must I code it manually
PostPosted: Sat Dec 20, 2003 8:22 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
gloeglm wrote:
Simple: Load the Author first from the DB, if it does not allready exist create a new one and save it. Then add the Article to the collection.


Thks for your reply.

You means I must code myself to fix it?

It is that:
Code:
String authorname=request.getParameter("authorname");
Auhor au=null;
Author au=session.find("from Author au where au.name=?",authorname,Hibernate.STRING);
if(au.isEmpty()){
   au=new Author();
   au.setName(authorname);
   Article art=new Article();
   art.setComment(...);
   au.addArticle(art);
   session.save(au);
}else{
   au.addArticle(art);
   session.save(au);
}


You mean I must do this every time? Is there more easy method to do this.For example, Can I do this in xml ?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes you have to. But use session.update() if the Author allready exists. Or use session.saveOrUpdate()


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:38 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
gloeglm wrote:
Yes you have to. But use session.update() if the Author allready exists. Or use session.saveOrUpdate()


It seems I have to do this. :(

I change the code to:
Code:

String authorname=request.getParameter("authorname");
Auhor au=null;
Author au=session.find("from Author au where au.name=?",authorname,Hibernate.STRING);
if(au.isEmpty()){
   au=new Author();
   au.setName(authorname);
   Article art=new Article();
   art.setComment(...);
   au.addArticle(art);
   session.save(au);
}else{
   au.addArticle(art);
   session.update(au);   // or use session.saveOrUpdate(au);
}

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This obviously wont work, session.find() returns a collection, no Author object. But I'm sure you will figure this out yourself :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:40 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Thks

Would you tell me when I must use proxy in hibernate? I had been puzzled all the time after I saw the example in hibernate documents.

It use a proxy in hiber.hbm.xml. I really don't know what to do with that? How to use "proxy"?

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you use a proxy for a class, hibernate won't load the class completely upon load (for example when it is used as a property of another object), but rather lazily when it is first acessed.


Top
 Profile  
 
 Post subject: sorry. I don't know your meaning exactly
PostPosted: Sat Dec 20, 2003 8:50 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
gloeglm wrote:
If you use a proxy for a class, hibernate won't load the class completely upon load (for example when it is used as a property of another object), but rather lazily when it is first acessed.



You mean proxy can lazy the object? But I can lazy the object using "lazy=true" in XX.hbm.xml <set .... . ></set>

Another mean is if a class have a property of another class.will it use proxy? I don't know how to use it exactly. :(

_________________
You are not alone...


Top
 Profile  
 
 Post subject: Re: sorry. I don't know your meaning exactly
PostPosted: Sat Dec 20, 2003 8:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Quote:
You mean proxy can lazy the object? But I can lazy the object using "lazy=true" in XX.hbm.xml <set .... . ></set>


No, you can lazy load a collection. To lazy load a class, you need a proxy.

Quote:
Another mean is if a class have a property of another class.will it use proxy?


Yes

Quote:
I don't know how to use it exactly. :(


Then just try it.[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 8:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
And read http://www.hibernate.org/hib_docs/reference/html/performance.html#performance-s4


Top
 Profile  
 
 Post subject: Thks
PostPosted: Sat Dec 20, 2003 9:02 pm 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Thks in advanced !


I will read it carefully :)

_________________
You are not alone...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.