2007年1月28日 星期日

Spring + Hibernate Annotation

Hibernate必需要使用Hibernate 3.2.X以上

一般來說用了Spring架構後,會針對Bean撰寫xxx.hbm.xml檔,及在Spring的XML檔撰寫sessionFactory

如果要在Spring搭配使用Hibernate Annotation,可分兩種環境

純綷的Spring,步驟如下:

1.先將hibernate-annotation.jar及ejb-persistence.jar導入專案,這裏須注意的是ejb-persistence.jar檔通常有很多版本,但重點是裏面要javax.persistence.* package,像我就是用ejb-3.0-public-draft-20060118.jar

2.將sessionFactory的類型改為org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

3.在sessionFactory裏加上欲對應的model清單...

<property name="annotatedClasses">
<list>
<value>欲對應的model名稱(包含package,例onlyfun.model.User)</value>
</list>
</property>


4.建立hibernate.cfg.xml,範例如下:

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration>
 <session-factory>
  <mapping class="欲對應的model名稱(包含package,例onlyfun.model.User)"/>
 </session-factory>

</hibernate-configuration>



透過Hibernate的config,步驟如下:

1.建立Hibernate.cfg.xml文件,範例如下:

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration>
 <session-factory>
  <mapping class="欲對應的model名稱(包含package,例onlyfun.model.User)"/>
 </session-factory>

</hibernate-configuration>

2.改變Spring config文件中的sessionFactory配置:


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocations">

<list>
<value>hibernate.cfg.xml文件路徑(如果有個文件則已逗號(,)隔開)</value>
</list>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>

21 則留言:

匿名 提到...

Undeniably believe that which you stated. Your favorite justification appeared
to be on the web the simplest thing to be aware of.
I say to you, I certainly get annoyed while people think about worries
that they plainly don't know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people can take a signal. Will probably be back to get more. Thanks
Here is my blog http://www.erumeli.com

匿名 提到...

Hi i am kavin, its my first occasion to commenting anywhere, when i read this piece of writing i thought i
could also create comment due to this good piece of writing.
Also visit my page - Fast Payday Loan

匿名 提到...

Having read this I thought it was really enlightening. I appreciate you taking the time and effort to put this informative
article together. I once again find myself personally spending way too much time both reading and leaving comments.
But so what, it was still worthwhile!
Take a look at my weblog - Fast loans 1080

匿名 提到...

whoah this weblog is magnificent i really like reading your posts.
Stay up the great work! You know, many persons are
searching around for this information, you can help them greatly.
Feel free to visit my blog - laqualist.com

匿名 提到...

These are actually wonderful ideas in regarding blogging.
You have touched some pleasant things here. Any way keep up wrinting.
My web-site ... instantpaydayloans

匿名 提到...

Appreciate the recommendation. Let me try it out.
Here is my weblog ... www.ministrymingle.com

匿名 提到...

In fact no matter if someone doesn't know afterward its up to other visitors that they will assist, so here it occurs.
My web blog ... gosearchcity.biz

匿名 提到...

Pretty nice post. I just stumbled upon your weblog and wished to
say that I've truly enjoyed surfing around your weblog posts. After all I'll be subscribing on your rss feed and I hope you
write once more very soon!
My homepage - Quick Loan 560

匿名 提到...

This design is steller! You most certainly know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to
start my own blog (well, almost...HaHa!) Excellent job.
I really enjoyed what you had to say, and more
than that, how you presented it. Too cool!
Here is my blog post www.bitslapd.com

匿名 提到...

I think the admin of this web site is truly working hard in
favor of his website, since here every data is quality
based material.
Here is my web site - Fast Cash Loan 444

匿名 提到...

There is certainly a great deal to learn about this subject.
I really like all the points you have made.
Also see my website :: Cybernet4U.Com

匿名 提到...

Heya i'm for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me.
My weblog ; www.jsgxsy.com

匿名 提到...

Hi, I do believe this is a great blog. I stumbledupon it ;) I may revisit once again since I book
marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.
Review my webpage Same Day Loan 118

匿名 提到...

What a material of un-ambiguity and preserveness of precious
knowledge about unpredicted feelings.
Feel free to surf my web page - quick payday loan

匿名 提到...

At this moment I am ready to do my breakfast, when having my breakfast coming over again to read further news.
Here is my page - http://www.yabadaba.com.ar

匿名 提到...

Good way of describing, and nice article to obtain information regarding my presentation focus, which i am going to deliver in college.
My website :: Fastcashloan

匿名 提到...

Thanks to my father who informed me on the topic of this weblog, this weblog is really amazing.
Here is my blog post ... www.turkolog.de

匿名 提到...

Wow, awesome blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your website is excellent, let alone
the content!
My web blog :: quickloan560

匿名 提到...

My brother suggested I might like this blog. He was totally right.
This post actually made my day. You cann't imagine simply how much time I had spent for this information! Thanks!
Also visit my blog post meta-world.com

匿名 提到...

Excellent post. I am facing some of these issues as well.

.
Feel free to visit my page ... Orgs.Utulsa.edu

匿名 提到...

Howdy! I know this is somewhat off topic but I
was wondering which blog platform are you using for this site?
I'm getting fed up of Wordpress because I've had problems with hackers and I'm looking at options for another platform. I would be great if you could point me in the direction of a good platform.

Check out my webpage; buy 500 uk facebook likes