现在的问题是checkbox只取一个值的时候,能插入到数据库,但取多个值的时候就插不进去了!!!
jsp页面关键代码:
<input type="hidden" value="${paper.paperId}" name="paperchoice.paperId" />
<input type="checkbox" name="paperchoice.choiceId" value="${choice.choiceId}" />
bean层实体类:
 PaperChoice 
package bean;import java.util.HashSet;
import java.util.Set;/**
 * PaperChoice entity. @author MyEclipse Persistence Tools
 */public class PaperChoice implements java.io.Serializable { // Fields private Integer id;
private Integer choiceId;
private Integer paperId;
private Set paperChoices = new HashSet(0); // Constructors
/** default constructor*/

/** minimal constructor */
public PaperChoice(Integer choiceId,Integer paperId){
this.choiceId=choiceId;
this.paperId=paperId;
}
/** full constructor */
   public PaperChoice(Integer choiceId,Integer paperId,Set paperChoices){
   this.choiceId=choiceId;
   this.paperId=paperId;
   this.paperChoices=paperChoices;
   }
   
 
    // Property accessors public Integer getChoiceId() {
return choiceId;
} public void setChoiceId(Integer choiceId) {
this.choiceId = choiceId;
} public Integer getPaperId() {
return paperId;
} public void setPaperId(Integer paperId) {
this.paperId = paperId;
} /** default constructor */
public PaperChoice() {
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} }数据库操作:
public boolean addPaperChoice(PaperChoice paperchoice) {
Long i=null; Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.save(paperchoice);

tx.commit();
}
catch (HibernateException e)
{
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
throw e;
}finally{
HibernateSessionFactory.closeSession();
} return true;
}
action:
public String addPaperChoice(){
}
paperService.addPaperChoice(paperchoice);
return "success";
}

解决方案 »

  1.   

    调试看看,addPaperChoice为空吗
    或看下控制台有没有报什么错。
      

  2.   

     爆的异常是取到的choiceId为空 但单条记录是可以插入的 多条就不行了  
    org.hibernate.PropertyValueException: not-null property references a null or tra
    nsient value: bean.PaperChoice.choiceId
      

  3.   

    checkbox取一个值的时候,服务器端收到的是单个值,
    取多个值时,收到的是一个数组,你再用单值的方法去处理,当然有问题以上只的我猜测你的问题,你贴的代码看不出你在哪里选择了多个checkbox
      

  4.   

      第二个input啊 列出了所有的choiceId 然后在里面选择添加几个choiceId的 图片贴不上来 请问用数组是怎么处理我这种情况的啊?  获得checkbox的值,并传入数组中 ,然后将数组值转化为字符串 ,就可以存入数据库了 关键是这个字符串和数据库的字段不知道怎么匹配了。有没有例子参考一下的?
      

  5.   


    private Integer choiceId;把这个定义成数组:private Integer[] choiceId;在插入数据库的时候,就需要自己处理了,是用逗号连接还是要怎么,只能靠自己了。
      

  6.   

    1、jsp中的两个input,在实体类中都是Integer,如果你准备把其中的一个存储多选框的值,那么应该用String或是List2、在jsp页面中,增加提交前的验证方法,比如用alert输出选中的checkbox的值3、在后台,输出checkbox的值2和3的值比较以下,就能找到原因在哪了。
      

  7.   

    要在checkedbox取多值的话,你可以在action中加入这段话获取:
    String[] choiceIdArray = request.getParamters("这里放接收表单的name");
    上面这句是返回一个数组,存放你checkedbox勾选的多个值。
      

  8.   

    public String addPaperChoice(){
    }这个大括号是怎么回事啊?不会报红色xx么?
    paperService.addPaperChoice(paperchoice);
    return "success";
    }