【问题标题】:Help with Hibernate mapping帮助 Hibernate 映射
【发布时间】:2010-06-01 19:41:50
【问题描述】:

我有以下课程

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 时,我收到以下错误

在目标 VM 中发生异常:未能延迟初始化角色集合:com.rssFeed.domain.RSS.rssItems,没有会话或会话已关闭 org.hibernate.LazyInitializationException:未能延迟初始化角色集合:com.rssFeed.domain.RSS.rssItems,没有会话或会话已关闭 在 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) 在 com.rssFeed.dao.hibernate.HibernateRssDao.get(HibernateRssDao.java:47) 在 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) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 在 org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) 在 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) 在 com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) 在 com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) 在 com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) 在 com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) 在 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.execute(DefaultProtocolChain.java:88) 在 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.run(ContextTask.java:69) 在 com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) 在 com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) 在 java.lang.Thread.run(Thread.java:619)

这是什么意思?

谢谢

我根据 Pascal 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 =“false”时,我才会收到上一个错误

顺便说一下,这是我的表定义

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;

【问题讨论】:

  • java.sql.SQLException: Table not found in statement ... 看起来RSSItem 表不存在。

标签: hibernate


【解决方案1】:

基本上,当您尝试访问延迟加载的属性并且 Hibernate Session 已关闭(因此无法加载该属性,因此出现异常)时,会引发 LazyInitializationException。这正是跟踪报告的关于 rssItems 属性的内容。

奇怪的是你用lazy="false" 标记了这个属性,根据文档,这禁用延迟获取并指定总是急切地获取关联所以rssItems 应该得到急切获取,你不应该得到LazyInitializationException

话虽如此,我不确定映射是否正确,我不确定映射java.util.Collection 时是否可以使用&lt;set&gt;。我找不到官方参考,但如果我没记错的话,您应该使用&lt;bag&gt; 映射Collection。那么你可以尝试一下吗:

  • rssItems属性的类型改为java.util.Set~or~
  • 更改您的映射以使用&lt;bag&gt;

完成后,检查rssItems 在检索RSS 实体时是否按预期快速加载(在此激活Hibernate SQL 日志记录会有所帮助)。

【讨论】:

  • 是的,我也想一直获取 RssItems。我会试试你的建议谢谢
  • 关于表格,你是对的,它拼写错误,但我仍然收到错误没有会话或会话已关闭
  • 当我调试并查看 Rss 对象时,Rssitem 被定义为 PersistentBag 并且它一直在评估,一段时间后它会抛出异常 no session
【解决方案2】:

您的表在 DB 中名为 RSS_ITEM,但 hibernate 正在 SQL 查询中查找 RSSItem(检查错误)。您需要在映射中修复表名。请参考 hbm XML 中的第二类映射。此处表名应为 RSS_ITEM。

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2010-11-18
    • 1970-01-01
    相关资源
    最近更新 更多