将多个<h:selectonemenu>结果获取到列表失败无法将类型为type的实体转换为[interface java.util.List]

时间:2019-04-17 08:36:35

标签: jsf primefaces

编辑:我用尝试编辑代码的人尝试发送所有选择值,但仅发送最后一个失败。

我在使用primefaces,jsf,jpa的项目中遇到了一些麻烦。 我有一个页面显示一个称为角色的实体表,在这里我尝试在以前出售的某个角色上制作一些ipnuts。我有一些基本的输入(健康,黄金等),这些工作正常。但是我的问题是我尝试选择在成就实体中找到的成就并将其存储到称为characterAchievement的联接表中的问题。可选值的显示有效。转换器的getAsObject()检索选定的成就实现以检索选定的项目。但是当我尝试将其发送到控制器时,它总是会失败。

我收到此错误。

坟墓:javax.faces.component.UpdateModelException:javax.el.E​​LException:/character.xhtml @ 77,55 value =“#{characterBean.characterAchievement}”:无法转换[be.ezgame.model.Achievement@d7fdfc0类型的[class be.ezgame.model.Achievement]转换为[interface java.util.List]

我非常绝望,试图解决这个问题好几天了,但是没有任何效果。请帮助我。

这是我的characterBean

private static final long serialVersionUID = 8310888927201817145L;
private EntityManager em;
private List<Character> listCharacter;
private List<Character> selectedCharacter;
private Character characterNew;
private List<Character> leaderboard;
private List<Achievement> characterAchievement;
private List<CharacterAchievement> characterAchievements;
private CharacterAchievement charAchiev;

public CharacterBean() {
}

@PostConstruct
public void init() {
    characterNew = new Character();
    charAchiev = new CharacterAchievement();
    characterAchievements = new ArrayList<CharacterAchievement>();
    characterAchievement = new ArrayList<Achievement>();
    em = EMF.getEM();
    CharacterService cService = new CharacterService(em);
    listCharacter = cService.findAll();
    leaderboard = cService.findLeaderboard();
    em.close();
}

public void checkRowSelect() {
    if (selectedCharacter.size() < 1 || selectedCharacter.size() > 3) {
        System.out.println("Validation error " + selectedCharacter.size());
        selectedCharacter.clear();
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN,
                "Select at least one item & no more than 3",
                ""));
    } else {
        for(int i = 0; selectedCharacter.size() > i; i++) {
            charAchiev.setCharacter(selectedCharacter.get(i));
            System.out.println("char achievement Char " + charAchiev.getCharacter());
            characterAchievements.add(charAchiev);
            System.out.println("char achievement Char " + characterAchievements.get(i).getCharacter());
        }

        System.out.println("Validation ok " + selectedCharacter.size());
        return;
    }
}

public void submitCharacterUpdate() {
    em = EMF.getEM();
    CharacterService cService = new CharacterService(em);
    System.out.println("Character list size " + selectedCharacter.size());
    System.out.println("Achievements list size " + characterAchievement.size());

    /*
    for (int i = 0; selectedCharacter.size() > i; i++) {
        System.out.println("Character name to update " + selectedCharacter.get(i).getCharacterName());
        if(selectedCharacter.get(i).getCharacterHitPoint() == 0) {
            selectedCharacter.get(i).setCharacterStatus(false);
        }
        em.getTransaction().begin();
        cService.update(selectedCharacter.get(i));
        em.getTransaction().commit();
    }*/
    selectedCharacter.clear();
    characterNew = new Character();
    listCharacter = cService.findAll();
    em.close();
    return;
}

public void submitCancel() {
    characterNew = new Character();
}

The getters & setters

这是character.xhtml(添加了整个页面,以便更清楚地描述我的问题)

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    template="/ressources/templates/template.xhtml">
    <ui:define name="head">
        <title>#{i18n['characters']}</title>
    </ui:define>
    <ui:define name="content">
        <div>
            <h3 style="text-align: left;">#{i18n['characters']}</h3>
        </div>

        <p:separator />
        <h:form id="form">

            <p:dataTable id="checkboxDT"
                style="width: 80%; margin-left: auto; margin-right: auto; text-align:center"
                var="character" value="#{characterBean.listCharacter}"
                selection="#{characterBean.selectedCharacter}"
                rowKey="#{character.characterId}">
                <f:facet name="header">
                    <h:outputText value="#{i18n['character.list']}" />
                </f:facet>

                <p:column selectionMode="multiple"
                    style="width:16px;text-align:center" />
                <p:column headerText="#{i18n['character.name']}">
                    <h:outputText value="#{character.characterName}" />
                </p:column>
                <p:column headerText="#{i18n['character.experience']}">
                    <h:outputText value="#{character.characterExperience}" />
                </p:column>
                <p:column headerText="#{i18n['character.gold']}">
                    <h:outputText value="#{character.characterGold}" />
                </p:column>
                <p:column headerText="#{i18n['character.hitPoint']}">
                    <h:outputText value="#{character.characterHitPoint}" />
                </p:column>
                <p:column headerText="#{i18n['character.status']}">
                    <h:outputText
                        rendered="#{ (character.characterStatus) eq 'false' }"
                        value="#{i18n['character.dead']}" />
                    <h:outputText rendered="#{ (character.characterStatus) eq 'true' }"
                        value="#{i18n['character.alive']}" />
                </p:column>
                <p:column width="5%">
                    <p:commandButton process="checkboxDT" icon="fa fa-fw fa-edit"
                        update="@form:characterDetail"
                        actionListener="#{characterBean.checkRowSelect}"
                        oncomplete="PF('characterDetail').show();">
                        <f:setPropertyActionListener value="#{character}"
                            target="#{characterBean.selectedCharacter}" />
                    </p:commandButton>
                </p:column>
            </p:dataTable>

<p:dialog header="#{i18n['character']}" widgetVar="characterDetail"
    dynamic="true" closable="false" resizable="false" showEffect="fade"
    hideEffect="fade">
    <h:panelGroup id="characterDetail">
        <p:messages autoUpdate="true" />
        <ui:repeat value="#{characterBean.selectedCharacter}"
            var="selectedCharacter">
            <h:outputLabel value="#{selectedCharacter.characterName}" />
            <hr />
            <h:panelGrid columns="2">
                <h:outputLabel value="#{i18n['character.gold']}" />
                <p:spinner value="#{selectedCharacter.characterGold}" />
                <h:outputLabel value="#{i18n['character.experience']}" />
                <p:spinner value="#{selectedCharacter.characterExperience}" />
                <h:outputLabel value="#{i18n['character.hitPoint']}" />
                <p:spinner value="#{selectedCharacter.characterHitPoint}" />
                <h:outputLabel value="#{i18n['achievements']}"/>
                <h:selectOneMenu id="achievementSelect" converter="#{achievementConverter}"
                    value="#{characterBean.characterAchievement[0]}">
                    <f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
                    <f:selectItems value="#{achievementBean.listAchievement}" var="a"
                        itemValue="#{a}" itemLabel="#{a.achievementName} + #{a.achievementDescription}"/>
                </h:selectOneMenu>
            </h:panelGrid>
            <hr />
        </ui:repeat>

        <h:panelGrid columns="2" style="width: 100%; text-align:center">
            <p:commandButton value="#{i18n['general.submit']}"
                icon="fa fa-check"
                actionListener="#{characterBean.submitCharacterUpdate}"
                oncomplete="updateForm()" />

            <p:commandButton value="#{i18n['general.cancel']}"
                icon="fa fa-close" action="#{characterBean.submitCancel}"
                oncomplete="PF('characterDetail').hide();" update="@form"
                process="@this" />
        </h:panelGrid>
        <p:remoteCommand name="updateForm" process="@this" update="@form" />
    </h:panelGroup>
</p:dialog>

        </h:form>
    </ui:define>
</ui:composition>

编辑:我添加了3个实体(为了更好的可读性,已将getters / setter保留)

@Entity(name="Characters")

public class Character implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="CHARACTER_ID")
    private Integer characterId;

    @Column(name="CHARACTER_EXPERIENCE")
    private int characterExperience;

    @Column(name="CHARACTER_GENDER")
    private boolean characterGender;

    @Column(name="CHARACTER_GOLD")
    private int characterGold;

    @Column(name="CHARACTER_HIT_POINT")
    private int characterHitPoint;

    @Column(name="CHARACTER_LEVEL")
    private int characterLevel;

    @Column(name="CHARACTER_NAME")
    private String characterName;

    @Column(name="CHARACTER_STATUS")
    private boolean characterStatus;

    //bi-directional many-to-one association to Classe
    @ManyToOne
    private Classe classe;

    //bi-directional many-to-one association to Race
    @ManyToOne
    private Race race;

    //bi-directional many-to-one association to Title
    @ManyToOne
    private Title title;

    //bi-directional many-to-one association to User
    @ManyToOne
    private User user;

    //bi-directional many-to-one association to CharacterAchievement
    @OneToMany(mappedBy="character")
    private List<CharacterAchievement> characterAchievements;

    //bi-directional many-to-one association to CharacterInventory
    @OneToMany(mappedBy="character")
    private List<CharacterInventory> characterInventories;

    //bi-directional many-to-one association to HallOfFame
    @OneToMany(mappedBy="character")
    private List<HallOfFame> hallOfFames;

    public Character() {
    }

我的成就实体

@Entity
public class Achievement implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="ACHIEVEMENT_ID")
    private Integer achievementId;

    @Column(name="ACHIEVEMENT_DESCRIPTION")
    private String achievementDescription;

    @Column(name="ACHIEVEMENT_NAME")
    private String achievementName;

    //bi-directional many-to-one association to Title
    @ManyToOne
    private Title title;

    //bi-directional many-to-one association to CharacterAchievement
    @OneToMany(mappedBy="achievement")
    private List<CharacterAchievement> characterAchievements;

    public Achievement() {
    }

}

我的性格成就实体

@Entity
@Table(name="character_achievement")
@NamedQuery(name="CharacterAchievement.findAll", query="SELECT c FROM CharacterAchievement c")
public class CharacterAchievement implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="CHARACTER_ACHIEVEMENT_ID")
    private int characterAchievementId;

    @Column(name="ACHIEVEMENT_STATUS")
    private byte achievementStatus;

    //bi-directional many-to-one association to Achievement
    @ManyToOne
    private Achievement achievement;

    //bi-directional many-to-one association to Character
    @ManyToOne
    private Character character;

    public CharacterAchievement() {
    }

}

0 个答案:

没有答案