I recently posted a post about filling out JTable, which is now fully functional. Be that as it may, I am now stuck on something else, I hope to get two specific subtleties with this thread.
One of them is to solve my problem, and the other is to prepare an answer when others stumble on the same problem, because I still have not found the right answer.
I have the following script
Two tables:
games
id pk int
genre int
title varchar ....
genre
id pk int
name varchar ..
There can only be one genre in one game, but one genre can contain many games.
I use Eclipse as my IDE and eclipselink as my JPA provider.
, eclipse, .
:
@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;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Genre", nullable=false)
private Genre genreBean;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Publisher", nullable=false)
private Publisher publisherBean;
@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;
@OneToMany(mappedBy="genreBean")
private List<Game> 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);
, , .
, , : id, title, genre-name, ,
JTable :
switch(columnIndex){
case 0:
return game.getTitle();
case 1:
return game.getPublisherBean().getPublisher();
case 2:
return game.getGenreBean().getGenre();
case 3:
return game.getReleaseDate();
}
JTable case 1 2 , .
,
Battlefield 3 empty empty 3-3-2011
, .
, , - , , select, , JOIN - .
, , , ^^
.