1

Recently I did [CRUD Enterprise Application in NetBeans][1.] I made my own scenario for it namely storing scores for player. I have tree tables player, game and playerGame table. Every thing works fine with player and game table (add, remove, edit) as in tutorial form youtube but, I have problem with linked table (playerGame). When I try generate JSF Form From Entity using netBeans Palette I am getting this code

<h:selectOneMenu id="fkGameId" value="#{controller.playerGame.fkGameId}" title="FkGameId" required="true" requiredMessage="The FkGameId field is required."> <!-- TODO: update below reference to list of available items--> <f:selectItems value="#{fixme}"/> </h:selectOneMenu>

Also I have tried this

                    <h:selectOneMenu id="fkPlayerId" value="#{controller.player.playerId}" title="FkPlayerId" required="true" requiredMessage="The FkPlayerId field is required.">
                    <!-- TODO: update below reference to list of available items-->
                    <f:selectItems value="#{controller.player.playerGameList}"/>
                </h:selectOneMenu>

but with no result

in place where foreign keys to playerId and gameId should be inputed by palette.

My question is what I have to do to fix this, it would be nice to see similar example on linked tables but I didn't found anything (only one simple table examples I have found) I am newbie in JavaEE and this was my first tutorial ever in this subject and to be honest I don't know how to name thing properly.

Also trying to find the solution for the problem I did JavaServer Faces tutorial form netbeans website Generating JSF CRUD Application form Databases. The tutorial was pretty straight forward and I had no problem with it and it works fine with the same scenario (I was able to select foreign keys form dropdown list and create playerGame record). I have tried solve this problem based on JSF pages form this tutorial but the pages are generated using netBeans command JSF Pages form Entity Classes I because I have no experience in Java EE like mentioned before I have failed to solve the problem.

P.S. I you need me to provide more information please just ask.

UPDATE:

It is partially working now thanks to Geinmachi

Partial SOLUTION:

In my controller class I have added

    private String playerList;

public String getPlayerList() {
    return playerList;
}

public void setPlayerList(String playerList) {
    this.playerList = playerList;
}



private List<Player> playerLists;

public List<Player> getPlayerLists() {
    return playerLists;
}

public void setPlayerLists(List<Player> playerLists) {
    this.playerLists = playerLists;
}


@PostConstruct
public void init(){
   playerLists = playerFacade.findAll();
    // playerLists = new ArrayList<>();
   // playerLists.add( new Player(player.getPlayerId()));
}
public void submit(){
    System.out.println("Selected Player: " + playerList);
}

and my jsf code looks like

                    <h:selectOneMenu  value="#{controller.playerList}">
                    <f:selectItem itemValue="#{null}" itemLabel="#{player.playerName}"/>
                    <f:selectItems value="#{controller.playerLists}" var="player" itemValue="#{player.playerId}" itemLabel="#{player.playerName}"/>
                </h:selectOneMenu>

the was found in one of links given by Geinmachi.

However now I am getting javax.ejb.EJBException: Transaction aborted when trying to insert data to database because partial solution doesn't get fk_game_id.

1 Answers1

2

Given fields in PlayerGame entity (with getters/setters)

  • private Player fkPlayerId
  • private Game fkGameId

In controller:

  • private List<Player> playerList
  • private List<Game> gameList

form:

<h:form>
        <h:outputText value="Choose player"/>
        <h:selectOneMenu value="#{controller.playerGame.fkPlayerId}" converter="playerConverter">
            <f:selectItems value="#{controller.playerList}" var="player" 
                           itemValue="#{player}" itemLabel="#{player.playerName}"/>
        </h:selectOneMenu>
        <h:outputText value="Choose Game"/>
        <h:selectOneMenu value="#{controller.playerGame.fkGameId}" converter="gameConverter">
            <f:selectItems value="#{controller.gameList}" var="game" 
                           itemValue="#{game}" itemLabel="#{game.gameName}"/>
        </h:selectOneMenu>
        <h:commandButton action="#{controller.createPlayerGame}" value="Link player with a game"/>
    </h:form>

In your controller you need lists of all players and all games to choose from. You have to create converters for entities because on the HTML page they are just String and JSF expects Player and Game objects. For POJO JSF converter you can search in google and read here

See:

Community
  • 1
  • 1
Geinmachi
  • 1,251
  • 1
  • 8
  • 20
  • In my PlayerGame.java I have private Player fkPlayerId. You mean this field etc.? –  Dec 25 '15 at 23:06
  • Yes, in this case you need converter. – Geinmachi Dec 25 '15 at 23:13
  • I did found also this [netbeans link](http://wiki.netbeans.org/DevelopJavaEE6App#Creating_the_Enterprise_Application_Projects) there is getDiscountCodes method, so I have to create something similar in my controller class right ?? –  Dec 25 '15 at 23:16
  • No, you don't have to. You can just bind a `List` to ``, no need to create programmatic model. – Geinmachi Dec 25 '15 at 23:24
  • Updated my answer a bit. If you have entites in `h:selectOneMenu` you need converter, you should find plenty of examples in google when searching for "jsf converter". – Geinmachi Dec 25 '15 at 23:49
  • It is working now Big thanks for help –  Dec 26 '15 at 01:35