我正在尝试添加用户在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列时,我是否只想添加一个值?
答案 0 :(得分:1)
你有两个问题:
解决问题:
markSectionOne
的名称。您的类的任何方法都不应该包含相同的字段名称。您可以将其更改为saveMarkSectionOne
或有助于代码可读性的内容。 为您班级中的markSectionOne
字段创建获取者和设置者。这些方法可以通过getXxx
和setXxx
来识别。在这种情况下,方法如下:
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>
对视图中的其他字段和组件执行类似操作。
最后但并非最不重要的一点是,您还应该在将新实例插入数据库后将其分配给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();
}