jiangzhen8492 2009-05-11 15:25
浏览 256
已采纳

hibernate List 配置问题

question.java
[code="java"]package org.jiangzhen.domain;

import java.util.List;

/**

  • Question entity.
  • @author MyEclipse Persistence Tools */

public class Question implements java.io.Serializable {

// Fields
/**
 * 
 */
private static final long serialVersionUID = -2690686686167706680L;
private Integer id;
private String questionname;
private List<Item> items;

// Constructors
public List<Item> getItems() {
    return items;
}

public void setItems(List<Item> items) {
    this.items = items;
}

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

/** full constructor */
public Question(String questionname) {
    this.questionname = questionname;
}

// Property accessors

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

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

public String getQuestionname() {
    return this.questionname;
}

public void setQuestionname(String questionname) {
    this.questionname = questionname;
}

}[/code]

Item.java
[code="java"]package org.jiangzhen.domain;

/**

  • Item entity.
  • @author MyEclipse Persistence Tools */

public class Item implements java.io.Serializable {

// Fields
/**
 * 
 */
private static final long serialVersionUID = 5085907386974277395L;
private Integer id;
private String itemname;
private Integer questionid;
private Integer indexitem;
private Question question;

// Constructors

public Question getQuestion() {
    return question;
}

public void setQuestion(Question question) {
    this.question = question;
}

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

/** full constructor */
public Item(String itemname, Integer questionid, Integer indexitem) {
    this.itemname = itemname;
    this.questionid = questionid;
    this.indexitem = indexitem;
}

// Property accessors

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

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

public String getItemname() {
    return this.itemname;
}

public void setItemname(String itemname) {
    this.itemname = itemname;
}

public Integer getQuestionid() {
    return this.questionid;
}

public void setQuestionid(Integer questionid) {
    this.questionid = questionid;
}

public Integer getIndexitem() {
    return this.indexitem;
}

public void setIndexitem(Integer indexitem) {
    this.indexitem = indexitem;
}

}[/code]

question.hbm.xml
[code="java"]







    <list name="items" table="item" cascade="all-delete-orphan" inverse="false" lazy="true">
    <key column="questionid"></key>
    <index column="indexitem"></index>
    <one-to-many class="org.jiangzhen.domain.Item"></one-to-many>
    </list>

</class>

[/code]

item.hbm
[code="java"]







</class>


[/code]

questionAction.java
[code="java"]private String questionname;

private List<Item> items;
private QuestionService questionService;

public String getQuestionname() {
    return questionname;
}

public void setQuestionname(String questionname) {
    this.questionname = questionname;
}

public List<Item> getItems() {
    return items;
}

public void setItems(List<Item> items) {
    this.items = items;
}

public QuestionService getQuestionService() {
    return questionService;
}

public void setQuestionService(QuestionService questionService) {
    this.questionService = questionService;
}

public String add(){
    try{

    Question question = new Question();
    question.setQuestionname(questionname);
    question.setItems(items);
    this.questionService.addQuestion(question);
    }catch(Exception e)
    {
        e.printStackTrace();

    }
    return SUCCESS;


}

[/code]

add.jsp
[code="java"]

<s:form action="add.action" >
<s:textfield name="questionname" label="name"></s:textfield>
<s:textfield name="items" label="items"></s:textfield>
<s:textfield name="items" label="items"></s:textfield>
<s:submit></s:submit>

</s:form>

[/code]

不知道这样配置对不对, 我想添加QUESTION表的数据时,ITEM表里的相应数据也同时添加, 现在都插入不进去,请大家帮帮我!! 是不是我的配置有问题~~ 我感觉有错误,比如ITEM 表里的itemname 如何得到值阿? 请大家指点一下, 怎么配置,才能实现2个表都能同时插入数据

  • 写回答

1条回答 默认 最新

  • xiaoyaolang5254238 2009-05-21 17:29
    关注

    第 20 行 private List items;

    改为: private List items=new ArrayList();

    还有依个地方也要new 一下。
    错误原因:你只是声明,却没有实例,要new 一下。

    如果还是不行,可以换成set试试,我这里有set的成功案例。
    用set替换list 我没有用过list 但是我用set很成功

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗