1

I have entity classes from database one of them is join table which has

@JoinColumn(name = "fk_game_id", referencedColumnName = "game_id")
@ManyToOne(optional = false)
private Game fkGameId;

I want to convert Game fkGameId(which is Integer in its own table) to integer so I will be able to make constructor like

public PlayerGame(Integer id, Integer fkGameId, Integer fkPlayerId){ ... }

My question is how to do this? the examples on internet are black magic to me I was looking at this example link here but I couldn't figure out where I have to create this converter? Inside entity class ?? how to actually trigger the converter where to do conversion, where to insert the code can anyone answer this question ??

UPDATE:

I think I need somehow do the conversion to resolve this problem, I did change public Game fkgameid to public Integer fkgameid but I was getting validation error saying that i want to send non entity as a entity value

In session facade i have this with error caused by playerGame.getFKGameID :

    private List<PlayerGameDetails> copyPlayerGamesToDetails(List<PlayerGame> playerGames) {
    List<PlayerGameDetails> list = new ArrayList<PlayerGameDetails>();
    Iterator i = playerGames.iterator();
    while (i.hasNext()) {
        PlayerGame playerGame = (PlayerGame) i.next();
        Integer fkgameid = (Integer) playerGame.getFkGameId();
        PlayerGameDetails details = new PlayerGameDetails(playerGame.getPlayerGameId(),
                playerGame.getPlayerGameScore(), playerGame.getFkGameId(), playerGame.getFkPlayerId());
        list.add(details);
    }
    return list;
} 

with error game cannot be converted to integer on g and p and also this:

    @Override
public void createPlayerGame(PlayerGameDetails details) {
    try {
        Integer g = (Integer) details.getFkGameId();
        Integer p = (Integer) details.getFkPlayerId();
        PlayerGame playerGame = new PlayerGame(details.getPlayerGameId(), details.getPlayerGameScore(), g, p);
        em.persist(playerGame);
    } catch (Exception ex) {
        throw new EJBException(ex);
    }
}

with error saying Integer cannot be converted to Game

Community
  • 1
  • 1
  • 1
    Entity Framework in Java? Probaby you mean JPA or Hibernate – Luiggi Mendoza Jan 04 '16 at 03:37
  • The example you linked is for JSF and I strongly suspect that that is not related to what you are trying to do. – Suhas K Jan 04 '16 at 03:41
  • JPA i think, i have managed bean, session facade with remote interface, generally enterprise application with web app client and java class library etc. Everything is straight forward with tables without foreign keys but I couldn't make this work for join table –  Jan 04 '16 at 03:46

1 Answers1

0

You shouldn't use any converter. Keep the ManyToOne association

Assuming you want to create a PlayerGame for an existing game and an existing player, instead of defining

public PlayerGame(Integer id, Integer fkGameId, Integer fkPlayerId)

define

    public PlayerGame(Integer id, Game game, Player player)

(although your ID should rather be auto-generated, and thus not be part of the constructor)

Instead of using

PlayerGame pg = new PlayerGame(1, 2, 3);

Use

PlayerGame pg = new PlayerGame(1, 
                               entityManager.getReference(Game.class, 2),
                               entityManager.getReference(Player.class, 3));

Note that, if game and player are the only fields of that entity, it could be ditched completely, and replaced by a ManyToMany association between Player and Game.

I would also rename the field fkGameId to game. What you have there is not an ID, but an instance of Game. fk stands for foreign key, which is a relational database concept, not an OO concept.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • this doesn't work for me but I was able to figure this out with @SkilletBread's help. I will mark your answer as a solution to this problem as a thanks for your effort! And update the question with the solution later on. –  Jan 04 '16 at 19:17