0

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 のみが更新されます。何がそのような問題を引き起こす可能性がありますか? コードが十分でない場合は、お知らせください。

4

1 に答える 1

1

このような状況に陥った可能性がある人は、getter は DB ロジックを実装するためのものではないことに注意してください。私の場合、遅延ロードしました

public String getSlide() {
    if(slide == null)
        slide = getFromDB();

    return slide;
}

そしてそれは私の問題を解決しました。その質問を指摘してくれてありがとう@johny。

于 2013-04-03T12:10:53.770 に答える