I had recently posted someone about populating a JTable, which is now fully functional. How ever, I am stuck over something else now, I'm hoping to achieve two specific thigns with this thread.
One is to solve my problem, and the other is to have an answer ready for when others stumble across the same problem, cause I never found a proper answer to this yet.
I have the following scenario
Two tables:
games
id pk int
genre int
title varchar ....
genre
id pk int
name varchar ..
One game can only have one genre, but one genre can contain many games.
I'm using Eclipse as my IDE and eclipselink as my JPA provider.
I have generated the classes with the entity wizard provided by eclipse, whom also allowed me to define relationships between the two tables.
The relationships look as follows:
@Entity
@Table(name="games")
public class Game implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="Id", unique=true, nullable=false)
private int id;
@Temporal( TemporalType.DATE)
@Column(name="AddDate", nullable=false)
private Date addDate;
@Lob()
@Column(name="Description", nullable=false)
private String description;
@Temporal( TemporalType.DATE)
@Column(name="ModifiedDate", nullable=false)
private Date modifiedDate;
@Temporal( TemporalType.DATE)
@Column(name="ReleaseDate", nullable=false)
private Date releaseDate;
@Column(name="Title", nullable=false, length=255)
private String title;
//bi-directional many-to-one association to Genre
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Genre", nullable=false)
private Genre genreBean;
//bi-directional many-to-one association to Publisher
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Publisher", nullable=false)
private Publisher publisherBean;
and the genre
@Entity
@Table(name="genres")
public class Genre implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="Id", unique=true, nullable=false)
private int id;
@Column(name="Genre", nullable=false, length=150)
private String genre;
//bi-directional many-to-one association to Game
@OneToMany(mappedBy="genreBean")
private List<Game> games;
I use the following to select all games
List<Game> games;
try{
TypedQuery<Game> selectGamesQuery = entityManager.createQuery("SELECT g FROM Game g", Game.class);
games = selectGamesQuery.getResultList();
} catch(Exception e) {
games = null;
System.out.println(e);
The problem here is, List games does not contain the genre name, actually, it is empty.
How do I configure the list to contain a game that has the following attributes: id, title, genre-name, publisher-name, releasedate
In my JTable I have tried the following:
switch(columnIndex){
case 0:
return game.getTitle();
case 1:
return game.getPublisherBean().getPublisher();
case 2:
return game.getGenreBean().getGenre();
case 3:
return game.getReleaseDate();
}
In this case, in my JTable the columns for case 1 and 2 are empty, while the rest works.
So my table content looks like
Battlefield 3 empty empty 3-3-2011
I think with this I should have provided enough info.
Please let me know if there is any mistake or error in the code, or perhaps the select query, as I couldn't even find if I HAVE to write an explicit JOIN query and that it doesnt do that due to the defined relationship.
I'm still a noobie, so please be gentle ^^
Thank you for your time.