我正在尝试使用 JSF 2、Hibernate 开发 Web 应用程序,而对于服务器,我正在使用 JBoss AS 7。我有一个实体 Slide 和一个 bean SliderAction。我可以成功地将实体添加到我的数据库并删除它们,但是在编辑实体时会引起麻烦。在编辑页面上,我填充我的实体并显示给用户进行编辑,当用户提交时,只有实体的最后一个属性得到更新。
这是 Slide.class
@Entity
public class Slide extends AbstractEntity<Integer>{
private static final long serialVersionUID = 1L;
private String title;
private String description;
private Image image;
private String link;
private Page page;
private boolean showOnFeed;
private boolean approved;
private boolean cropped;
public String getTitle() {
return title;
}
public void setTitle(String title) {
System.out.println( getId() + " = TITLE = " + title );
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
System.out.println( getId() + " = DESC = " + description );
this.description = description;
}
@OneToOne
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@OneToOne
public Page getPage() {
return page;
}
public void setPage(Page page) {
System.out.println( getId() + " = PAGE = " + page.getHeading() );
this.page = page;
}
@Column(name="show_on_feed")
public boolean isShowOnFeed() {
return showOnFeed;
}
public void setShowOnFeed(boolean showOnFeed) {
this.showOnFeed = showOnFeed;
}
public boolean isApproved() {
return approved;
}
public void setApproved(boolean approved) {
this.approved = approved;
}
public boolean isCropped() {
return cropped;
}
public void setCropped(boolean cropped) {
this.cropped = cropped;
}
}
SliderAction.class
@Named
@RequestScoped
public class SliderAction {
private EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("db");
private EntityManager entityManager;
private int id;
private int pageId;
private Integer coordinates[] = new Integer[4];
private Image image;
private Slide slide = new Slide();
private List<Slide> slides = new ArrayList<Slide>();
private List<Slide> publicSlides = new ArrayList<Slide>();
private UploadedFile uploadedFile;
private boolean externalLink = false;
private boolean editable = true;
public String add() {
return "add";
}
public String addSlide() {
ImageAction imageAction = new ImageAction();
imageAction.upload(uploadedFile, false);
image = imageAction.getImage();
slide.setImage( image );
slide.setCropped( false );
startTransaction();
if( !externalLink ) {
slide.setPage(entityManager.find(Page.class, pageId));
}
entityManager.persist(image);
entityManager.persist(slide);
endTransaction();
closeTransaction();
setId(slide.getId());
return "details";
}
public void editSlide() {
System.out.println(slide);
startTransaction();
entityManager.merge(slide);
endTransaction();
closeTransaction();
}
public void deleteSlide() {
startTransaction();
slide = (Slide)entityManager.find(Slide.class, id);
entityManager.remove(slide);
endTransaction();
closeTransaction();
slides.remove(slide);
ImageAction imageAtion = new ImageAction();
imageAtion.delete(slide.getImage().getUniqueName(), slide.getImage().getThumb());
}
public void makeEditable() {
editable = true;
}
public void startTransaction() {
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
}
public void endTransaction() {
entityManager.getTransaction().commit();
}
public void closeTransaction() {
entityManager.close();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Slide getSlide() {
if( id != 0 ){
startTransaction();
slide = entityManager.find(Slide.class, id);
endTransaction();
closeTransaction();
}
return slide;
}
public void setSlide(Slide slide) {
this.slide = slide;
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
@SuppressWarnings("unchecked")
public List<Slide> getSlides() {
startTransaction();
slides = entityManager.createQuery("from Slide").getResultList();
System.out.println(">>>>>>>>>>>>>>>>>>SLIDES");
endTransaction();
closeTransaction();
return slides;
}
public void setSlides(List<Slide> slides) {
this.slides = slides;
}
public Integer[] getCoordinates() {
return coordinates;
}
public void setCoordinates(Integer[] coordinates) {
this.coordinates = coordinates;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public boolean isExternalLink() {
return externalLink;
}
public void setExternalLink(boolean externalLink) {
this.externalLink = externalLink;
}
@SuppressWarnings("unchecked")
public List<Slide> getPublicSlides() {
startTransaction();
publicSlides = entityManager.createQuery("from Slide where showOnFeed = true").getResultList();
return publicSlides;
}
public void setPublicSlides(List<Slide> publicSlides) {
this.publicSlides = publicSlides;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
public int getPageId() {
return pageId;
}
public void setPageId(int pageId) {
this.pageId = pageId;
}
}
编辑页面:
<html>
<!-- DOCTYPE specification -->
<f:metadata>
<f:viewParam name="id" value="#{sliderAction.id}"/>
</f:metadata>
<ui:composition template="/view/private/administration/administrator.xhtml">
<ui:define name="title">#{msgs.addingSlide}</ui:define>
<ui:define name="extra-head">
<h:outputStylesheet library="css" name="jquery.jcrop.min.css"/>
</ui:define>
<ui:define name="content">
<div class="grid_14">
<h4 class="page-heading"><h:outputText value="#{sliderAction.slide.title} #{msgs.details}" /></h4>
<div class="line clear"></div>
<h:panelGroup id="details">
<h:form id="edit-slide">
<h:panelGrid columns="2">
<h:outputText value="#{msgs.slideTitle}" styleClass="label"/>
<h:outputText value="#{sliderAction.slide.title}" rendered="#{not sliderAction.editable}"/>
<h:inputText id="title" value="#{sliderAction.slide.title}" required="true" rendered="#{sliderAction.editable}"/>
<p></p><h:message for="title" errorClass="error"/>
<h:outputText value="#{msgs.description}" styleClass="label"/>
<h:outputText value="#{sliderAction.edidSlide.description}" rendered="#{not sliderAction.editable}"/>
<h:inputTextarea id="description" value="#{sliderAction.slide.description}" required="true" rendered="#{sliderAction.editable}"/>
<p></p><h:message for="description" errorClass="error"/>
<h:outputText value="#{msgs.page}" styleClass="label" />
<h:link outcome="notset" rendered="#{not sliderAction.editable}">
<h:outputText value="#{sliderAction.slide.page.heading}"/>
<f:param name="id" value="#{sliderAction.slide.page.id}" />
</h:link>
<h:selectOneMenu id="page" value="#{sliderAction.pageId}" required="true" rendered="#{sliderAction.editable}">
<f:selectItems value="#{pageAction.pages}" var="page" itemLabel="#{page.heading}" itemValue="#{page.id}"/>
</h:selectOneMenu>
<p></p><h:message for="page" errorClass="error"/>
<p></p>
<h:commandButton action="#{sliderAction.makeEditable}" value="#{msgs.edit}" styleClass="button" rendered="#{not sliderAction.editable}">
<f:ajax render="@form" execute="@this" />
</h:commandButton>
<h:commandButton action="#{sliderAction.editSlide}" value="#{msgs.saveChanges}" styleClass="button" rendered="#{sliderAction.editable}">
<f:ajax render="@form" execute="@form" />
</h:commandButton>
<h:message for="edit-slide"/>
</h:panelGrid>
</h:form>
</h:panelGroup>
</div>
</ui:define>
这里当用户提交时,只有 slide.description 被更新。什么会导致这样的问题?如果代码还不够,请告诉我。