除了I.D之外,没有数据添加到数据库

时间:2014-02-19 17:32:39

标签: java jsf jpa jsf-2

我正在尝试添加用户在u.i上选择的值。到数据库中的特定列,我已经连接到数据库工作,当我按下保存时,新的id被添加到数据库中,控制台中没有显示错误,但我想提交的值没有放入数据库,我怎么能实现这个目标?

这是gui的代码

                               <p:spinner id="ajaxspinner80-100" value="#{markingBean.spinnerNumber1}" 
                                           stepFactor = "1"  min="80" max="100" disabled = "#{formBean.number != 8}">  
                                    <p:ajax update="ajaxspinnervalue" process="@this" />  
                                </p:spinner> 
                    <p:commandButton action="#{markingBean.markSectionOne}" value="#{bundle.buttonSave}" update=":growl" icon="ui-icon-disk"/>

这是一个名为markingBean的bean

 private Marking markSectionOne;

    private MarkingService markingService;

    @Inject
    private MarkingFacade markingFacade;

    @PostConstruct
    public void init() {
        this.markSectionOne = new Marking();
    }

    public void markSectionOne() {
        this.markingFacade.create(this.markSectionOne);
        this.setMessage("Mark Saved");
    }

abstractFacade中的create类

public void create(T entity) {
    getEntityManager().persist(entity);
}

这会导致id添加到数据库中,但不会将markSectionOne标记添加到名为markSectionOne的列中,为什么会这样,

用于标记的实体类是:

@Entity(name = "MARKING")
public class Marking implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String markingStage, markingCompleted, markSectionOne, markSectionTwo, markSectionThree, markSectionFour, markSectionFive, overalMark, plagorism, feedback, comments;

我得到错误的唯一一次是当我点击保存两次时,我在控制台中收到错误:

WARNING:   Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL140219165944320' defined on 'MARKING'.
Error Code: -1
Call: INSERT INTO MARKING (ID, COMMENTS, FEEDBACK, MARKSECTIONFIVE, MARKSECTIONFOUR, MARKSECTIONONE, MARKSECTIONTHREE, MARKSECTIONTWO, MARKINGCOMPLETED, MARKINGSTAGE, OVERALMARK, PLAGORISM) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [12 parameters bound]

当有12列时,我是否只想添加一个值?

1 个答案:

答案 0 :(得分:1)

你有两个问题:

  1. 您不会在要插入数据库的对象的字段中填充数据。
  2. 您尝试插入上一个事务中使用的同一个对象,该事件包含自动生成的ID。由于此id是表的主键,因此在尝试插入具有已插入ID的新行时,它将为您提供与重复的唯一键或主键相关的错误消息。这是错误消息的相关部分:唯一或主键约束中的重复键值
  3. 解决问题:

    1. 更改您的方法markSectionOne的名称。您的类的任何方法都不应该包含相同的字段名称。您可以将其更改为saveMarkSectionOne或有助于代码可读性的内容。
    2. 为您班级中的markSectionOne字段创建获取者和设置者。这些方法可以通过getXxxsetXxx来识别。在这种情况下,方法如下:

      public Marking getMarkSectionOne() {
          return this.markSectionOne;
      }
      public void setMarkSectionOne(Marking markSectionOne) {
          this.markSectionOne = markSectionOne;
      }
      

      完成这些更改后,您现在可以使用JSF的强大功能轻松地将数据绑定到视图中。假设int spinnerField类中有一个Marking字段及其各自的getter和setter,你可以将它绑定到微调器的值,如下所示:

      <!--
          This acts as markingBean.getMarkSectionOne.getSpinnerField() when rendering
          and as markingBean.getMarkSectionOne.setSpinnerField(spinner.getValue()) when sending the request to the server
      -->
      <p:spinner id="ajaxspinner80-100" value="#{markingBean.markSectionOne.spinnerField}"
          stepFactor="1"  min="80" max="100" disabled="#{formBean.number != 8}">
          <p:ajax update="ajaxspinnervalue" process="@this" />
      </p:spinner>
      

      对视图中的其他字段和组件执行类似操作。

    3. 最后但并非最不重要的一点是,您还应该在将新实例插入数据库后将其分配给markSectionOne字段。您可以通过在saveMarkSectionOne方法中创建新实例来实现此目的:

      public void markSectionOne() {
          //supposing the data in markSectionOne is filled...
          this.markingFacade.create(markSectionOne);
          this.setMessage("Mark Saved");
          //after saving...
          markSectionOne = new Marking();
      }