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个表都能同时插入数据

第 20 行 private List items;

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

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

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