Reputation: 2155
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.
Upvotes: 3
Views: 1327
Reputation: 2155
The answer has been resolved.
The problem I had, and the reason I didnt get anything, was because the value was null. It was a very sloppy mistake of my own in the database. I added a record to genre id 2 for some reason rather then id 1 while the game had a genre id of 1, which was empty
(thank you Kris Babic) :)
So my valueable lesson of the day is: Dubble check results in the database, see if you have matching records. Use
<properties>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
When debugging
And I have added insert functionality. So thank you for that Jorge.
Thanks for taking the time to answer and read.
So apperantly, the relationships work very nicely using JPA. ALOT nicer then linq to sql in my opinion.
I can now call the following:
game.getPublisherBean().getPublisher();
And I get the wanted result. So "RPG" instead of ""
Upvotes: 1
Reputation: 18237
are you sure about how you are doing the inserts?? because could happen that when you insert the game you need to insert the genre in the list and update the game someting like this
/**** the var em is the entity manager ****/
Game game = new Game();
game.setId(1);
game.setDesription("This is a example");
game.setTitle("try");
em.persist(game);
Genre genre = new Genre();
genre.setName("blah");
genre.setId(1);
em.persist(genre);
then you need to take the entity already persistence and set to each object because the relation is bidirectional something like this
genre.getGenreBean().add(game);
em.persist(genre);
i hope that this example help you with the problem
EDIT:
If you need the query it's something like this
select G, Game from Genre, IN(g.getGenreBean()) Game
This query return a Collection of Object[] in the position 0 have the genre and the position 1 the game
Upvotes: 1
Reputation: 30025
to get a bit closer to the problem you could enable finegrained JPA logging by adding the following in your persistence.xml (inside a persistence-unit element):
<properties>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
Then watch your log file and look how the jpql-query gets translated to sql.
Another idea (may sound stupid!): Did you set the foreign key constraints in the database too or only in Persistence Unit?
Hope this helps
Upvotes: 2