【发布时间】:2014-03-20 05:26:53
【问题描述】:
我正在尝试添加用户在 u.i 上选择的值。到数据库中的特定列,我已经建立了与数据库的连接,当我按下保存时,一个新的 id 被添加到数据库中,控制台中没有显示错误,但是我要提交的值没有被放入数据库,我怎么能做到这一点?
这是图形用户界面的代码
<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 列时,我是否只想添加一个值?
【问题讨论】:
-
是否调用了托管 bean 操作?实体管理器是否保留数据?有错误信息要查看吗?
-
根本没有错误消息要查看,我得到标记保存的 ajax 消息,控制台中什么都没有,并且 i.d.已添加到数据库中
-
另外应该注意的是 i.ds 不是一个接一个的,它们是随机的
-
看起来您没有在对象中填充数据。此外,请确保在保存后将
Marking的新实例分配给您的markSectionOne,这是在您的托管 bean 中。 -
内部
public void markSectionOne:markSectionOne.setMarkingStage("foo"); this.markingFacade.create(this.markSectionOne); markSectionOne = new Marking();...