struts2中的action取到了值,但插入不到数据库中

J2EE 码拜 9年前 (2015-04-11) 1155次浏览 0个评论
 

我们用的是ssh2框架搭建的,要做一个物理实验系统。需要在预习题库中插入题目,用的是xheditor编辑器。

action是这个:
public String saveQuestion() throws IOException{
try{
PreviewLibrary question = new PreviewLibrary();
question.setModel(1);//题目类型 integer的 1是多选题(无所谓啦)
question.setTitle(title);//题目内容string
question.setAnswer(answer);//答案也是string
question.setExperiment(experimentService.getExperiment(experimentId));//在数据表中预习题的外键是experiment_id,在实体类中就是experiment啦
if ( previewId== null || previewId <= 0) {
this.pagePrint(previewLibraryService.savePreviewLibrary(question));//因为写了个baseDao,这个最终调用的就是session里面的savaorupdate方法啦,另外pagePrint这个函数有一个返回值,供前台的ajax回调用的,“1”就是成功
} else {
}
}catch(Exception e){
this.pagePrint(“0”);//我们的程序现在就是进这里面去了我已经debug了一天了,好难过,求大神帮帮我
e.printStackTrace();
throw new RuntimeException();
}
return NONE;
}

实体类Previewlibrary
public class PreviewLibrary implements java.io.Serializable {

private static final long serialVersionUID = 1L;
/** 预习题id */
private Long previewId;
/** 所属实验  */
private Experiment experiment;
/** 预习题型:0=单选,1=多选,2=判断 */
private Integer model;
/** 题目 */
private String title;
/** 答案:单选、多选————由选项拼接成字符串(eg:“A,B,C,D”)
判断————0=对、1=错 */
private String answer;
//下面三个就不是必须的了
/** 出题人id */
private Long peopleId;
/** 创建时间 */
private Timestamp createTime;
/** 修改时间 */
private Timestamp modifyTime;
//这个是previewlibraryDaoImp里面有关的方法
public void savePreviewLibrary(PreviewLibrary previewLibrary) {
this.saveOrUpdate(previewLibrary);
//         this.save(previewLibrary);
}
最后效果是这个
struts2中的action取到了值,但插入不到数据库中
报的错有这些
struts2中的action取到了值,但插入不到数据库中
还有java.lang.illegalStateException
org.hibernate.genericJDBCException,如果还需要什么我可以继续上传,真心求助,全部积分了!

struts2中的action取到了值,但插入不到数据库中
50分
Hibernate的xml配置文件中关于主键生成策略可能没写对,检查下previewId的属性配置。
struts2中的action取到了值,但插入不到数据库中
30分
previed_id 数据库是非空的,而你又没有为其赋值,所以爆掉了。
一般将previed_id做自增长就可以了(逻辑主键)。
struts2中的action取到了值,但插入不到数据库中
引用 2 楼 bree06 的回复:

previed_id 数据库是非空的,而你又没有为其赋值,所以爆掉了。
一般将previed_id做自增长就可以了(逻辑主键)。

可是hibernate不是会自动为oid赋值吗?

struts2中的action取到了值,但插入不到数据库中
引用 1 楼 zhangjihao 的回复:

Hibernate的xml配置文件中关于主键生成策略可能没写对,检查下previewId的属性配置。

<hibernate-mapping>
    <class name=”cn.edu.bjfu.phy.model.PreviewLibrary” table=”preview_library” catalog=”physical_exp”>
        <id name=”previewId” type=”java.lang.Long”>
            <column name=”preview_id” />
            <generator class=”identity” />
        </id>
        <many-to-one name=”experiment” class=”cn.edu.bjfu.phy.model.Experiment” fetch=”select”>
            <column name=”experiment_id” not-null=”true” />
        </many-to-one>
        <property name=”model” type=”java.lang.Integer”>
            <column name=”model” not-null=”true”>
            </column>
        </property>
        <property name=”title” type=”java.lang.String”>
            <column name=”title” length=”65535″ not-null=”true” />
        </property>
        <property name=”answer” type=”java.lang.String”>
            <column name=”answer” length=”10″ not-null=”true” />
        </property>
        <property name=”peopleId” type=”java.lang.Long”>
            <column name=”people_id” />
        </property>
        <property name=”createTime” type=”java.sql.Timestamp”>
            <column name=”create_time” length=”19″ />
        </property>
        <property name=”modifyTime” type=”java.sql.Timestamp”>
            <column name=”modify_time” length=”19″ />
        </property>
    </class>
</hibernate-mapping>
这个是配置文件 ,我们的主键生成策略都是identity,这样可以吗? 
这个是experiment的实体类和hbm文件
public class Experiment implements java.io.Serializable {

// Fields

private static final long serialVersionUID = 1L;
/** 实验id */
private Long experimentId;
/** 实验名称 */
private String experimentName;
/** 实验内容 */
private String content;
/** 实验标志 :1=必做实验,2=选课通称,3=选修实验*/
private Integer experimentTag;
/** 选修是否开始:1=没开始;2=开始 */
private Integer selectExpTag;
/** 创建时间 */
private Timestamp createTime;
/** 修改时间 */
private Timestamp modifyTime;
/** 预习题库 */
private Set previewLibraries = new HashSet(0);
/** 学生选修的实验 */
private Set peoples = new HashSet(0);
/** 该实验的课程表 */
private Set courses = new HashSet(0);
/** 实验成绩表 */
private Set scores = new HashSet(0);

<hibernate-mapping>
    <class name=”cn.edu.bjfu.phy.model.Experiment” table=”experiment” catalog=”physical_exp”>
        <id name=”experimentId” type=”java.lang.Long”>
            <column name=”experiment_id” />
            <generator class=”identity” />
        </id>
        <property name=”experimentName” type=”java.lang.String”>
            <column name=”experiment_name” length=”100″ not-null=”true” />
        </property>
        <property name=”content” type=”java.lang.String”>
            <column name=”content” length=”65535″ />
        </property>
        <property name=”experimentTag” type=”java.lang.Integer”>
            <column name=”experiment_tag” not-null=”true”>
            </column>
        </property>
        <property name=”selectExpTag” type=”java.lang.Integer”>
            <column name=”select_exp_tag”>
            </column>
        </property>
        <property name=”createTime” type=”java.sql.Timestamp”>
            <column name=”create_time” length=”19″ />
        </property>
        <property name=”modifyTime” type=”java.sql.Timestamp”>
            <column name=”modify_time” length=”19″ />
        </property>
        <set name=”previewLibraries” inverse=”true”>
            <key>
                <column name=”experiment_id” not-null=”true” />
            </key>
            <one-to-many class=”cn.edu.bjfu.phy.model.PreviewLibrary” />
        </set>
        <set name=”peoples” inverse=”true”>
            <key>
                <column name=”experiment_id” />
            </key>
            <one-to-many class=”cn.edu.bjfu.phy.model.People” />
        </set>
        <set name=”courses” inverse=”true”>
            <key>
                <column name=”experiment_id” not-null=”true” />
            </key>
            <one-to-many class=”cn.edu.bjfu.phy.model.Course” />
        </set>
        <set name=”scores” inverse=”true”>
            <key>
                <column name=”experiment_id” />
            </key>
            <one-to-many class=”cn.edu.bjfu.phy.model.Score” />
        </set>
    </class>
</hibernate-mapping>

struts2中的action取到了值,但插入不到数据库中
20分
previewId 你这个没有赋值,而且这个字段在数据库表中是非空的,没有默认值,所以报错
struts2中的action取到了值,但插入不到数据库中
我解决啦  就是你们说的那个问题 你们太棒啦! 我不小心把数据库的previewlibrary的主键的自动递增给勾掉了。。。
谢谢!  以后我就没多少积分了,还怎么提问啊

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明struts2中的action取到了值,但插入不到数据库中
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!