第一个实体/*
 * @(#)XyuNews.java        1.0 2011-9-14
 *
 * Copyright (c) 1998-2011 ChinaCache 
 * All rights reserved.
 *
 */package com.smartdot.icm.models.xyu.model;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;/**
 * 新闻信息,用来存储新闻的相关信息
 * 
 * @version 1.0 2011-9-14
 * @author Xyu
 * */@Entity
@Table(name = "XyuNews", schema = "HL")
@SequenceGenerator(name = "seq", allocationSize = 1, sequenceName = "news_seq")
public class XyuNews implements Serializable { /*
 * 本类通过使用Hibernate注解来实现独享关系映射
 */ /**
 * 新闻Id
 * */ private Integer newsId; /**
 * 新闻标题
 * */ private String newsTitle; /**
 * 新闻内容
 * */ private String newsContent; /**
 * 新闻时间
 * */ private Date newsWriteTime; /**
 * 新闻编写人
 * */ private String newsWriteName; /**
 * 新闻备注
 * */ private String newsRes; /**
 * 新闻的撰写人
 * */
private Set<XyuNewsWrite> xyunewswrites = new HashSet<XyuNewsWrite>(0);
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
public Integer getNewsId() {
return newsId;
} public void setNewsId(Integer newsId) {
this.newsId = newsId;
} public String getNewsTitle() {
return newsTitle;
} public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
} public String getNewsContent() {
return newsContent;
} public void setNewsContent(String newsContent) {
this.newsContent = newsContent;
} public Date getNewsWriteTime() {
return newsWriteTime;
} public void setNewsWriteTime(Date newsWriteTime) {
this.newsWriteTime = newsWriteTime;
} public String getNewsWriteName() {
return newsWriteName;
} public void setNewsWriteName(String newsWriteName) {
this.newsWriteName = newsWriteName;
} public String getNewsRes() {
return newsRes;
} public void setNewsRes(String newsRes) {
this.newsRes = newsRes;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "xyunews")
public Set<XyuNewsWrite> getXyunewswrites() {
return xyunewswrites;
} public void setXyunewswrites(Set<XyuNewsWrite> xyunewswrites) {
this.xyunewswrites = xyunewswrites;
}}第二个实体
/*
 * @(#)XyuNewsWrite.java        1.0 2011-9-19
 *
 * Copyright (c) 1998-2011 ChinaCache 
 * All rights reserved.
 *
 */package com.smartdot.icm.models.xyu.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;/**
 * 新闻撰写人信息,用来存储撰写人的相关信息
 * 
 * @version 1.0 2011-9-19
 * @author Xyu
 * */@Entity
@Table(name = "XyuNewsWrite")
@SequenceGenerator(name = "seq", allocationSize = 1, sequenceName = "news_write_seq")
public class XyuNewsWrite implements Serializable { /*
 * 本类通过使用Hibernate注解来实现独享关系映射
 */ /**
 * 撰写人Id
 * */ private Integer writeId; /**
 * 撰写人姓名
 * */ private String writeName; /**
 * 撰写人性别
 * */ private Integer writeSex; /**
 * 撰写人年龄
 * */ private Integer writeAge; /**
 * 撰写人简历
 * */ private String writeResume; /**
 * 撰写人今日得分
 * */ private String writeScore; /**
 * 新闻ID 外键
 * */ private XyuNews newsId;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
public Integer getWriteId() {
return writeId;
} public void setWriteId(Integer writeId) {
this.writeId = writeId;
} public String getWriteName() {
return writeName;
} public void setWriteName(String writeName) {
this.writeName = writeName;
} public Integer getWriteSex() {
return writeSex;
} public void setWriteSex(Integer writeSex) {
this.writeSex = writeSex;
} public Integer getWriteAge() {
return writeAge;
} public void setWriteAge(Integer writeAge) {
this.writeAge = writeAge;
} public String getWriteResume() {
return writeResume;
} public void setWriteResume(String writeResume) {
this.writeResume = writeResume;
} public String getWriteScore() {
return writeScore;
} public void setWriteScore(String writeScore) {
this.writeScore = writeScore;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "NEWSID", nullable = false)
public XyuNews getNewsId() {
return newsId;
} public void setNewsId(XyuNews newsId) {
this.newsId = newsId;
}}删掉OneToMany 程序正常, 单向正常, 但是加上就报错, 是不是配置有问题? 求助啊,
报错信息:删掉OneToMany 程序正常, 单向正常, 但是加上就报错, 是不是配置有问题? 求助啊,
QQ429720276 , 邮箱,[email protected]

解决方案 »

  1.   

    报错信息:
    2011-09-20 02:57:11,596 GalaxyContextLoaderPlugIn ERROR - Context initialization failed
    org.springframework.beans.factory.access.BootstrapException: Unable to return specified BeanFactory instance: factory key [icm],
    from group with resource name [classpath*:spring/beanRefContext.xml]; 
    nested exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'icm' defined in URL [file:/E:/Genuitec/Tomcat-5.5.29/Tomcat-5.5.29/webapps/ECM/WEB-INF/classes/spring/beanRefContext.xml]:
    Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
    Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]:
    Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'transactionTemplate' defined in class path resource [spring/spring-models-JCZC.xml]:
    Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager';
    nested exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'transactionManager' defined in class path resource [spring/hibernate-context.xml]:
    Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; 
    nested exception is org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'sessionFactory' defined in class path resource [spring/hibernate-context.xml]:
    Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property:
    com.smartdot.icm.models.xyu.model.XyuNewsWrite.xyunews in com.smartdot.icm.models.xyu.model.XyuNews.xyunewswrites
    at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:410)
    at com.smartdot.util.SpringUtils.getContext(Unknown Source)
    at com.smartdot.util.SpringUtils.getContext(Unknown Source)
    at com.smartdot.util.struts1.GalaxyContextLoaderPlugIn.setParentContext(GalaxyContextLoaderPlugIn.java:102)
    at com.smartdot.util.struts1.GalaxyContextLoaderPlugIn.initWebApplicationContext(GalaxyContextLoaderPlugIn.java:53)
    at org.springframework.web.struts.ContextLoaderPlugIn.init(ContextLoaderPlugIn.java:225)
    at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:869)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:336)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3996)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4266)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:980)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:943)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1203)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:319)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
      

  2.   

    问题解决,结贴OneToMany 要对相应ManyToOne那边的实体属性
      

  3.   

    呵呵,解决就好了,看来我就来接分了很久没用过hibernate呢祝楼主好运