2010-06-01 92 views
1

我有以下類幫助與Hibernate映射

public class RSS 
{ 
    private Integer id; 
    private String title; 
    private String description; 
    private String link; 
    private Date dateCreated; 
    private Collection rssItems; 
    private String url; 
    private String language; 
    private String rating; 
    private Date pubDate; 
    private Date lastBuildDate; 
    private User user; 
    private Date dateModified; 

    public RSS() { 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public void setDescription(String description){ 
     this.description = description; 
    } 

    public String getDescription(){ 
     return this.description; 
    } 

    public void setLink(String link){ 
     this.link = link; 
    } 

    public String getLink(){ 
     return this.link; 
    } 

    public void setUrl(String url){ 
     this.url = url; 
    } 

    public String getUrl(){ 
     return this.url; 
    } 

    public void setLanguage(String language){ 
     this.language = language; 
    } 

    public String getLanguage(){ 
     return this.language; 
    } 

    public void setRating(String rating){ 
     this.rating = rating; 
    } 

    public String getRating(){ 
     return this.rating; 
    } 

    public Date getPubDate() { 
     return pubDate; 
    } 

    public void setPubDate(Date pubDate) { 
     this.pubDate = pubDate; 
    } 

    public Date getLastBuildDate() { 
     return lastBuildDate; 
    } 

    public void setLastBuildDate(Date lastBuildDate) { 
     this.lastBuildDate = lastBuildDate; 
    } 

    public Date getDateModified() { 
     return dateModified; 
    } 

    public void setDateModified(Date dateModified) { 
     this.dateModified = dateModified; 
    } 

    public Date getDateCreated() { 
     return dateCreated; 
    } 

    public void setDateCreated(Date dateCreated) { 
     this.dateCreated = dateCreated; 
    } 

    public Collection getRssItems() { 
     return rssItems; 
    } 

    public void setRssItems(Collection rssItems) { 
     this.rssItems = rssItems; 
    } 
} 

public class RSSItem { 

    private RSS rss; 

    private Integer id; 
    private String title; 
    private String description; 
    private String link; 
    private Date dateCreated; 
    private Date dateModified; 
    private int rss_id; 

    public RSSItem() {} 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getLink() { 
     return link; 
    } 

    public void setLink(String link) { 
     this.link = link; 
    } 

    public Date getDateCreated() { 
     return dateCreated; 
    } 

    public void setDateCreated(Date dateCreated) { 
     this.dateCreated = dateCreated; 
    } 

    public Date getDateModified() { 
     return dateModified; 
    } 

    public void setDateModified(Date dateModified) { 
     this.dateModified = dateModified; 
    } 

    public RSS getRss() { 
     return rss; 
    } 

    public void setRss(RSS rss) { 
     this.rss = rss; 
    } 
} 

,我映射爲

<hibernate-mapping> 
    <class name="com.rssFeed.domain.RSS" schema="PUBLIC" table="RSS"> 
    <id name="id" type="int"> 
     <column name="ID"/> 
     <generator class="native"/> 
    </id> 
    <property name="title" type="string"> 
     <column name="TITLE" not-null="true"/> 
    </property>   
    <property name="lastBuildDate" type="java.util.Date"> 
     <column name="LASTBUILDDATE"/> 
    </property> 
    <property name="pubDate" type="java.util.Date"> 
     <column name="PUBDATE" /> 
    </property> 
    <property name="dateCreated" type="java.util.Date"> 
     <column name="DATECREATED" not-null="true"/> 
    </property> 
    <property name="dateModified" type="java.util.Date"> 
     <column name="DATEMODIFIED" not-null="true"/> 
    </property> 
    <property name="description" type="string"> 
     <column name="DESCRIPTION" not-null="true"/> 
    </property> 
    <property name="link" type="string"> 
     <column name="LINK" not-null="true"/> 
    </property> 
    <property name="url" type="string"> 
     <column name="URL" not-null="true"/> 
    </property> 
    <property name="language" type="string"> 
     <column name="LANGUAGE" not-null="true"/> 
    </property> 
    <property name="rating" type="string"> 
     <column name="RATING"/> 
    </property> 
    <set inverse="true" lazy="false" name="rssItems"> 
     <key> 
     <column name="RSS_ID"/> 
     </key> 
     <one-to-many class="com.rssFeed.domain.RSSItem"/> 
    </set> 
    </class> 
</hibernate-mapping> 


<hibernate-mapping> 
    <class name="com.rssFeed.domain.RSSItem" schema="PUBLIC" table="RSSItem"> 
    <id name="id" type="int"> 
     <column name="ID"/> 
     <generator class="native"/> 
    </id> 
    <property name="title" type="string"> 
     <column name="TITLE" not-null="true"/> 
    </property> 
    <property name="description" type="string"> 
     <column name="DESCRIPTION" not-null="true"/> 
    </property> 
    <property name="link" type="string"> 
     <column name="LINK" not-null="true"/> 
    </property> 
    <property name="dateCreated" type="java.util.Date"> 
     <column name="DATECREATED"/> 
    </property> 
    <property name="dateModified" type="java.util.Date"> 
     <column name="DATEMODIFIED"/> 
    </property> 
    <many-to-one class="com.rssFeed.domain.RSS" fetch="select" name="rss"> 
     <column name="RSS_ID"/> 
    </many-to-one> 
    </class> 
</hibernate-mapping> 

但當我嘗試獲取的RSS我收到以下錯誤

異常發生在目標虛擬機:未能懶惰地初始化一個角色集合:com.rssFeed.domain.RSS.rssItems,沒有會話或會話被關閉 org.hibernate.LazyInitializationException:無法懶惰地初始化角色集合:com.rssFeed.domain.RSS.rssItems,沒有會話或會話已關閉 at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) 在org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) 在org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97) 在org.hibernate.collection.PersistentSet.size(PersistentSet。 java:139) at com.rssFeed.dao.hibernate.HibernateRssDao.get(HibernateRssDao.java:47) at com.rssFeed.ServiceImplementation.RssServiceImplementation.get(RssServiceImplementation.java:46) 在com.rssFeed.mvc.ViewRssController.handleRequest(ViewRssController.java:20) 在org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) 在org.springframework.web.servlet。 DispatcherServlet.doDispatch(DispatcherServlet.java:875) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431) at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) at javax.servlet.http.HttpServlet.service( HttpServlet.java:847) at org.apache.catalina。 core.StandardWrapper.service(StandardWrapper.java:1523) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 188) at com.sun.enterprise com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) 。 web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java: (CoyoteAdapter.java:233) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) at com.sun。 grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter。的java:170) 在com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) 在com.sun.grizzly.DefaultProtocolChain。執行(DefaultProtocolChain.java:88) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain。的java:76) 在com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) 在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) 在com.sun.grizzly.ContextTask。運行(ContextTask.java:69) at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:330) at com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java: 309) 在java.lang.Thread.run(Thread.java:619) <

這是什麼意思?

感謝

我已經根據從帕斯卡Thivent

現在建議修改我的代碼,我得到下面的異常

type Exception report 

message 

descriptionThe server encountered an internal error() that prevented it from fulfilling this request. 

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [com.rssFeed.domain.RSS.rssItems#3]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.rssFeed.domain.RSS.rssItems#3] 

root cause 

org.springframework.dao.InvalidDataAccessResourceUsageException: could not initialize a collection: [com.rssFeed.domain.RSS.rssItems#3]; nested exception is org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.rssFeed.domain.RSS.rssItems#3] 

root cause 

org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.rssFeed.domain.RSS.rssItems#3] 

root cause 

java.sql.SQLException: Table not found in statement [select rssitems0_.RSS_ID as RSS7_1_, rssitems0_.ID as ID1_, rssitems0_.ID as ID2_0_, rssitems0_.TITLE as TITLE2_0_, rssitems0_.DESCRIPTION as DESCRIPT3_2_0_, rssitems0_.LINK as LINK2_0_, rssitems0_.DATECREATED as DATECREA5_2_0_, rssitems0_.DATEMODIFIED as DATEMODI6_2_0_, rssitems0_.RSS_ID as RSS7_2_0_ from PUBLIC.RSSItem rssitems0_ where rssitems0_.RSS_ID=?] 

note The full stack traces of the exception and its root causes are available in the GlassFish v3 logs. 

只有當我刪除lazyLoad =「假」我得到以前的錯誤

順便說一句,這是我的表定義

drop table RSS_ITEM if exists 
drop table RSS if exists 

create table RSS (
    id integer identity primary key, 
    title varchar(128) not null, 
    description varchar(2048) not null, 
    link varchar(1024) not null, 
    url varchar(1024) not null, 
    language varchar(63) not null, 
    rating varchar(63), 
    pubDate date, 
    lastBuildDate date, 
    user_id integer, 
    dateCreated date not null, 
    dateModified date not null 
); 


create table RSS_ITEM (
    id integer identity primary key, 
    title varchar(128) not null, 
    description varchar(2048) not null, 
    link varchar(1024) not null, 
    rss_id integer not null, 
    dateCreated date not null, 
    dateModified date not null 
); 


alter table RSS_ITEM add constraint item_rss foreign key (rss_id) references RSS; 
alter table RSS add constraint rss_user foreign key (user_id) references RSS_USER; 
+0

'java.sql.SQLException:Table not found in statement ...'它看起來像表'RSSItem'不存在。 – 2010-06-01 21:53:24

回答

0

基本上,當您嘗試訪問延遲加載的屬性並且Hibernate Session已關閉(因此該屬性無法加載,因此是例外)時,會引發LazyInitializationException。這正是跟蹤報告有關rssItems財產的原因。

奇怪的是,你標記這個屬性lazy="false"並根據文檔,這禁止延遲抓取,並指定一直使用預先抓取所以rssItems應該得到預先抓取,你不應該得到一個LazyInitializationException

話雖這麼說,我不肯定的映射是正確的,我不知道映射java.util.Collection時,你可以使用一個<set>。我找不到官方的參考資料,但是如果我沒記錯的話,您應該使用<bag>來映射Collection。所以,可以嘗試兩種:

  • 變化rssItems財產爲java.util.Set類型〜或〜
  • 改變你的映射使用<bag>

一旦完成,檢查rssItems GET當您檢索一個RSS實體時(如果激活Hibernate SQL日誌記錄在這裏會很有幫助),按預期急切加載。

+0

是的,我想獲取RssItems以及所有的時間。我會嘗試您的建議 謝謝 – GigaPr 2010-06-01 21:34:46

+0

關於表是正確的它拼寫錯誤,但我仍然得到錯誤沒有會話或會話被關閉 – GigaPr 2010-06-01 22:17:01

+0

當我調試,我看着Rss對象Rssitem被定義爲PersistentBag,它不斷評估並在一段時間後拋出異常沒有會話 – GigaPr 2010-06-01 22:21:24

0

您的表在數據庫中名爲RSS_ITEM,但hibernate正在SQL查詢中查找RSSItem(檢查錯誤)。您需要在映射中修復表名。請參閱hbm XML中的第二類映射。這裏的表名應該是RSS_ITEM。