2

According to this post I have implemented generic converter based on toString conversion-error-setting-value-com-example-warehousecafebabe-for-null-conver however I am getting transaction aborted exception fk_game_id cannot be null. What I have to do to make it work?

JSF:

                <h:selectOneMenu id="fkGameId" value="#{controller.gameList}" converter="omnifaces.SelectItemsConverter" required="true" requiredMessage="The Game Id field is required.">
                    <f:selectItem itemValue="#{null}" itemLabel="#{game.gameDescription}"/>
                    <f:selectItems value="#{controller.gameLists}" var="game" itemValue="#{game.gameId}" itemLabel="#{game.gameDescription}"/>
                </h:selectOneMenu>
                <h:outputLabel value="Player ID: " for="fkPlayerId" />
                <h:selectOneMenu id="fkPlayerId" value="#{controller.playerList}" converter="omnifaces.SelectItemsConverter" required="true" requiredMessage="The Player ID field is required.">
                    <f:selectItem itemValue="#{null}" itemLabel="#{player.playerName}"/>
                    <f:selectItems value="#{controller.playerLists}" var="player" itemValue="#{player.playerId}" itemLabel="#{player.playerName}"/>
                </h:selectOneMenu>

Generic Converter in game entity class:

@Override
public String toString() {
    return "entities.Game[ gameId=" + gameId + " ]";
}

controller bean:

@PostConstruct
public void init() {
    this.gameLists = gameFacade.findAll();
    this.playerLists = playerFacade.findAll();
    this.game = new Game();
    this.player = new Player();

    this.game.setGameId(Integer.SIZE);
    this.player.setPlayerId(Integer.SIZE);

    this.playerGame.setFkGameId(game);
    this.playerGame.setFkPlayerId(player);

}

public String addPlayerGame() {

    this.playerGame = new PlayerGame();
    this.playerGameFacade.create(this.playerGame);


    return "index";
}
Community
  • 1
  • 1

1 Answers1

1

You may need to check whether the gameId and playerId are getting populated correctly in your controller in the addPlayerGame method. For the playerId and gameId are to get populated in the Player & Game object of your controller, shouldn't the h:selectOneMenus look like below:

<h:selectOneMenu id="fkGameId" value="#{controller.game.gameId}"

and

<h:selectOneMenu id="fkPlayerId" value="#{controller.player.playerId}"

Other thing I notice is, in the addPlayerGame() method you are resetting playerGame object using new PlayerGame() and passing it to PlayerGameFacade. Instead you should be passing instance variable playerGame as it is as below as it contains the user select Game and Player ids that we need to persist.

public String addPlayerGame() {
this.playerGameFacade.create(this.playerGame);
return "index";
}

Posting PlayerGameFacade class would help to further debug, if needed.

  • I include PlayerGameFacade in my question –  Dec 29 '15 at 08:05
  • Now when I commented out playerGame object I am getting this: Caused by: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: entities.Game[ gameId=32 ]. –  Dec 29 '15 at 08:09
  • @DummyGummy After that change now we see the PlayerGame object populated with right data as this exception is showing gameId as 32 (not null). Can you set the cascade PERSIST on Player and Game relationship in Game and Player object respectively and try. – Madhusudana Reddy Sunnapu Dec 29 '15 at 08:25
  • still the same i have changed all to persist in player and game –  Dec 29 '15 at 08:32
  • Can you post Player, Game, PlayerGame and PlayerFacade as well? – Madhusudana Reddy Sunnapu Dec 29 '15 at 08:35
  • they looks exactly the same as PayerGameFacade do you want entity classes ?? they where generated using create entity classes from database –  Dec 29 '15 at 08:37
  • Yes, would like to see the entity classes with the PERSIST option you have tried. By the way in the PlayerGameFacade I don't see create method,I believe it is defined in the super class and it is as simple as em.persist(...). Is it right? – Madhusudana Reddy Sunnapu Dec 29 '15 at 08:42
  • I don't see Cascase.PERSIST being set in the PlayerGame object. You need to set it on both `ManyToOne(optional = false)` annotations for fkGameId and fkPlayerId fields of PlayerGame object. Because once you call the persist method on PlayerGame object we want that to cascade to both Game & Player. – Madhusudana Reddy Sunnapu Dec 29 '15 at 08:52
  • OK I did change it and I have added cascade = CascadeType.PERSIST in every entity class and now I am getting Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details. –  Dec 29 '15 at 08:58
  • It is because you have specified `NotNull` constraint on `gameDate' of `Game` object and wherever it is used or populate the data as per your constraints. You may want to remove that constraint(s) and try or easier one is disable validation mode by setting NONE in persistence.xml. – Madhusudana Reddy Sunnapu Dec 29 '15 at 09:08
  • I did sett this validation mode to none but I still getting the same error –  Dec 29 '15 at 09:11
  • Not able to find the exact property to disable validation, it seems we need to set and/or and try. Please have a look at [link](https://developer.jboss.org/thread/236142?start=0&tstart=0) – Madhusudana Reddy Sunnapu Dec 29 '15 at 09:43
  • Still the same with both or one property –  Dec 29 '15 at 09:52
  • Looks like we are not able to get the exact property to disable validation. Can you try removing the NotNull annotation in Player, Game and PlayerGame object. I believe it is only in the Game object. If it succeeds it should work once find the exact property. – Madhusudana Reddy Sunnapu Dec 29 '15 at 09:54
  • this is what I am getting now after removing all not null annotations Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'game_date' cannot be null HOWEVER I am not inserting data to game table! I am tying to insert to playerGametable –  Dec 29 '15 at 10:04
  • Right doest someone come across with an working example not generated by NetBeans with join table (similar scenario to mine) so I can build my app based on working example?? ?? I have found this [link](https://forum.hibernate.org/viewtopic.php?f=9&t=1011908) but this doesn't solve my problem. –  Dec 29 '15 at 10:58
  • @DummyGummy one thing that I see is the playerId and gameId are not getting populated in the Player & Game object of your controller. If you look at the exception closely it printed gameId as `gameId=32` which is the default value of Integer.SIZE that you assigned in PostConstruct. In the JSP shouldn't the selectOneMenus look like ` – Madhusudana Reddy Sunnapu Dec 29 '15 at 11:55
  • Right I just underplayed the project and I have restarted the server and it works!!! also I haven't modify the files at all just JSF according to your proposal. @Madhusudana Reddy Sunnapu can you modify you answer by showing how the PostConstruct method should looks like and I will accept it as an answer ? Thank you for your help! –  Dec 29 '15 at 19:47
  • Ok now inserting it works but when I am trying to insert another record the same values are populated over and over even when I select different one from dropdown menu. I have to restart the project so I can insert different data any ideas ?? –  Dec 30 '15 at 00:03
  • Update when I edit the entity using editPlayerGame and I return to Index page the id of the player and game description stays the same but when I click edit the correct entities are displayed so this issues persists only on index page. I think this must be something with this selection list –  Dec 30 '15 at 00:24
  • That is cool. Modifying the JSP and PostConstruct method fimally did the trick.And regarding index page retaining the old selection is because of session scope managed bean. – Madhusudana Reddy Sunnapu Dec 30 '15 at 01:43
  • there is way around it so I can insert few playerGames at the time and see the results on index page during the same session (like with player and game) ?? or how I can return to index page and update it after adding a record >?? The records displayed on index page are correct after I restart the application. So the values inserted are correct but they are not updated in current session on index page –  Dec 30 '15 at 01:53
  • @DummyGummy Posting the index page and its controller would help. You may want to consider posting this as new question, as this one looks full with comments :). I suspect that the `index page` is populating the `PlayerGame` data from managed bean which has a `session scope`. If so, either we can reduce the scope that managed bean or update the `PlayerGame` in that managed bean. – Madhusudana Reddy Sunnapu Dec 30 '15 at 02:25
  • There is no need for new question I have solved the issue by changing the scope of managed beans as you suggested. I have change it to @RequestScoped. At the moment I am not facing any issues but just in case I will do some testing to check if it is working in 100%. Thanks for Help again. –  Dec 30 '15 at 02:56
  • right i have run the testing but it doesn't work so the new post will be on soon –  Dec 31 '15 at 21:01