0
回答
Hibernate多表关联插值,总是报错,其详情如下
终于搞明白,存储TCO原来是这样算的>>>   

我想要进行历史新闻的添加,因为在历史新闻表设计了关联,字段为dynastyId,在页面上进行插入数据的时候,总是提示无法插入 原因是在关联的字段老是为空,所以不能插入,请教各位到底是怎么回事,另外在进行多表插值的时候需要注意什么,为什么用<s:select>标签时到后台不能将string转化为Integer,间action对应方法中的注释。在转化之后提示有错误原因是传过来的值是空的,但是我不连数据库直接进行2个页面传递的时候又可以显示select中的值,谢谢,这个问题一直没有解决掉!

历史新闻表

CREATE TABLE `

h_humannews' (

`id`  int(11) NOT NULL AUTO_INCREMENT ,

`dynastyID`  int(11) NOT NULL ,

`name`  varchar(20)  NOT NULL ,

`pic`  varchar(50) ,

`comment`  varchar(40) NOT NULL ,

`content`  text ,

`newsTime`  timestamp  ,

`newsAuthor`  varchar(20) ,

`isRecommend`  int(11) NULL DEFAULT NULL ,

PRIMARY KEY (`id`)

)

package com.history.entity;

import java.sql.Timestamp;

/**
 * HumanNews entity. @author  MyEclipse Persistence Tools
 */

public class HumanNews implements java.io.Serializable {

	// Fields

	private Integer id;
	private Dynasty dynastyId;
	private String name;
	private String pic;
	private String comment;
	private String content;
	private Timestamp newsTime;
	private String newsAuthor;
	private Integer isRecommend;

	// Constructors

	/** default constructor */
	public HumanNews() {
	}

	/** minimal constructor */
	public HumanNews(Dynasty dynastyId, String name, String comment,
			Timestamp newsTime) {
		this.dynastyId = dynastyId;
		this.name = name;
		this.comment = comment;
		this.newsTime = newsTime;
	}

	/** full constructor */
	public HumanNews(Dynasty dynastyId, String name, String pic,
			String comment, String content, Timestamp newsTime,
			String newsAuthor, Integer isRecommend) {
		this.dynastyId = dynastyId;
		this.name = name;
		this.pic = pic;
		this.comment = comment;
		this.content = content;
		this.newsTime = newsTime;
		this.newsAuthor = newsAuthor;
		this.isRecommend = isRecommend;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

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

	public Dynasty getDynastyId() {
		return this.dynastyId;
	}

	public void setDynastyId(Dynasty dynastyId) {
		this.dynastyId = dynastyId;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPic() {
		return this.pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}

	public String getComment() {
		return this.comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Timestamp getNewsTime() {
		return this.newsTime;
	}

	public void setNewsTime(Timestamp newsTime) {
		this.newsTime = newsTime;
	}

	public String getNewsAuthor() {
		return this.newsAuthor;
	}

	public void setNewsAuthor(String newsAuthor) {
		this.newsAuthor = newsAuthor;
	}

	public Integer getIsRecommend() {
		return this.isRecommend;
	}

	public void setIsRecommend(Integer isRecommend) {
		this.isRecommend = isRecommend;
	}

}

朝代表

CREATE TABLE `h_dynasty` (

`id`  int(11) NOT NULL AUTO_INCREMENT ,

`cname`  varchar(20)  NOT NULL ,

`ename`  varchar(20)  NOT NULL ,

`parentID`  int(11) NOT NULL ,

`startTime`  varchar(20)  ,

`endTime`  varchar(20)  ,

PRIMARY KEY (`id`)

)

HumanNews.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.history.entity.HumanNews" table="h_humannews" catalog="history">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native"></generator>
        </id>
     
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
        <property name="pic" type="java.lang.String">
            <column name="pic" length="50" />
        </property>
        <property name="comment" type="java.lang.String">
            <column name="comment" length="40" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" length="600" />
        </property>
        <property name="newsTime" type="java.sql.Timestamp">
            <column name="newsTime" length="19" />
        </property>
        <property name="newsAuthor" type="java.lang.String">
            <column name="newsAuthor" length="20" />
        </property>
        <property name="isRecommend" type="java.lang.Integer">
            <column name="isRecommend" />
        </property>
        <many-to-one name="dynastyId" 
        			 column="dynastyID" 
        			 class="com.history.entity.Dynasty" 
        			 lazy="false"
        			 not-null="false"
        			 >
        
        </many-to-one>
    </class>
</hibernate-mapping>

Dynasty.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.history.entity.Dynasty" table="h_dynasty" catalog="history">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native"></generator>
        </id>
        <property name="cname" type="java.lang.String">
            <column name="cname" length="20" not-null="true" />
        </property>
        <property name="ename" type="java.lang.String">
            <column name="ename" length="20" not-null="true" />
        </property>
        <property name="parentId" type="java.lang.Integer">
            <column name="parentID" not-null="true" />
        </property>
        <property name="startTime" type="java.lang.String">
            <column name="startTime" length="20" />
        </property>
        <property name="endTime" type="java.lang.String">
            <column name="endTime" length="20" />
        </property>
    </class>
</hibernate-mapping>
action中对应的方法如下
public class HumanNewsAction extends ActionSupport implements ModelDriven<HumanNews> {
/**
 * 上传图片对应的属性
 * */
//	String dynastyId ;
	
	File pic ;
	String picFileName ;
	String picContentType;

	HumanNewsService huNewsService ;
	List<HumanNews> huNewsList ;
	List<HumanNews> subNewsList ;
	DynastyService dyService ;
	Dynasty dy ;
	HumanNews huNews = new HumanNews();
	public HumanNews getModel() {
		return huNews;
	}
	
	public String addHuNews() throws Exception
	{
		/**
		 * 上传图片的处理
		 * */
		System.out.println("人物信息:"+ huNews.getNewsTime());
		System.out.println("新人物信息:"+ huNews.getDynastyId());
		System.out.println("是否被推荐:"+ huNews.getIsRecommend());
		if(this.getPicFileName() != null && this.getPicFileName().length() > 0)
		{
			String tempFileName = Tools.getRndFileName() + Tools.getFileExtName(this.getPicFileName());
			String filename = ServletActionContext.getServletContext().getRealPath("/upload")
							  .replaceAll("\\\\", "/")+"/"+tempFileName;
			FileOutputStream fos = new FileOutputStream(filename);
			FileInputStream fis = new FileInputStream(this.getPic());
			byte[] buf = new byte[1024] ;
			int len = 0 ;
			while((len = fis.read(buf)) > 0)
			{
				fos.write(buf , 0 ,len);
			}
			if(fis != null)
			{
				fis.close();
			}
				
			if(fos != null)
			{
				fos.close();
			}
			huNews.setPic("/upload/" + tempFileName);
		}
		HumanNews tmpHuNews = new HumanNews();
		if(huNews.getDynastyId() == null)
		{
			huNews.setDynastyId(dyService.findById(22));
		}
//		Integer id = Integer.parseInt(this.huNews.getDynastyId().toString());
//		dy = dyService.findById(id) ;
//		if(dy != null)
//		{
//			System.out.println(dy.getCname());
//			tmpHuNews.setDynastyId(dy);
//		}
		tmpHuNews.setDynastyId(huNews.getDynastyId());
		tmpHuNews.setName(this.huNews.getName());
		tmpHuNews.setComment(this.huNews.getComment());
		tmpHuNews.setDynastyId(this.huNews.getDynastyId());
		tmpHuNews.setContent(this.huNews.getContent());
		tmpHuNews.setIsRecommend(this.huNews.getIsRecommend());
		tmpHuNews.setPic(this.huNews.getPic());
		tmpHuNews.setNewsAuthor(this.huNews.getNewsAuthor());
		tmpHuNews.setNewsTime(this.huNews.getNewsTime());
		
		
		System.out.println("人物信息图片路径:"+ tmpHuNews.getPic());
		System.out.println("人物信息shjian:"+ tmpHuNews.getNewsTime());
		System.out.println("人物名字:"+ tmpHuNews.getName());
		System.out.println("人物评价:"+ tmpHuNews.getComment());
		System.out.println("人物信息朝代:"+ tmpHuNews.getDynastyId());
		System.out.println("人物信息内容:"+ tmpHuNews.getContent());
		System.out.println("人物信息朝作者:"+ tmpHuNews.getNewsAuthor());
		huNewsService.addHuNews(tmpHuNews);
		
		return "addHuNews" ;
	}
}
jsp页面
addHuNews.jsp  
  
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="sx" uri="/struts-dojo-tags" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <sx:head/>
    
    <title>My JSP 'addHuNews.jsp' starting page</title>
    

  </head>
  
  <body>

		<h3>添加历史人物新闻信息</h3>
		<a href="listAllHuNews.action">查看所有历史人物新闻</a>

		<s:form action="addHuNews" method="post" name="addHuForm"> 
		<table>
			<tr>
				<td align="right">人物名称:</td>
				<td ><s:textfield  name="name"></s:textfield></td>
			</tr>
			<tr>
				<td align="right">人物评价:</td>
				<td align="left"><s:textfield label="人物评价" name="comment"></s:textfield></td>
			</tr>
			
			<tr>
				<td align="right">所属朝代;</td>
				<td align="left"><s:radio list="#{'1':'上古','2':'夏'}" listKey="key" listValue="value" name="dynastyId" value="2"></s:radio></td>
			</tr>
			
			
			 <!-- 
			<tr>
				<td align="right">所属朝代:</td>
				<td align="left"><s:select name="dynastyId" list="#{'1':'上古','2':'夏','3':'商','4':'西周','5':'春秋战国','8':'秦'
									,'9':'汉','16':'两晋南北朝','21':'隋','22':'唐','23':'宋','26':'元','27':'明','28':'清','29':'民国'}"	
								 listKey="key" listValue="value" value="22"></s:select></td>
			</tr>
			 --> 
			<tr>
				<td align="right">作者:</td>
				<td align="left" ><s:textfield label="作者" name="newsAuthor"></s:textfield></td>
			</tr>
			<tr>
				<td align="right">是否被推荐;</td>
				<td align="left"><s:radio list="#{'1':'是','0':'否'}" listKey="key" listValue="value" name="isRecommend" value="0"></s:radio></td>
			</tr>
			<tr>
				<td align="right">人物图片</td>
				<td align="left"><s:file name="pic"></s:file></td>
			</tr>
			<tr>
				<td align="right">新闻时间:</td>
				<td align="left"><sx:datetimepicker type="date" name="newsTime"/>
			</tr>
			<tr>
				<td align="right">新闻内容:</td>
				<td align="left"><s:textarea label="内容" name="content" cols="150 " rows="20"></s:textarea></td>
			</tr>
			<tr>
				<td>
					<s:submit  value="提交"></s:submit>
					
				</td>
					<td>
					<s:reset  value="重置"></s:reset>
					
				</td>
			</tr>
		</table>
		</s:form>

  </body>
 </html>
struts.xml
<!-- 添加历史人物信息 -->
<action name="addHuNews" class="HumanNewsAction" method="addHuNews">
	<result name="addHuNews">/admin/listAllHuNewsBack.jsp</result>
	<result name="input">/admin/addHuNews.jsp</result>
</action>

举报
crelaberq
发帖于5年前 0回/339阅
顶部