Java jpa for many

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;

//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;

@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;

,

        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 - .

, , , ^^

.

+3
3

, JPA, persistence.xml( persistence):

<properties>
      <property name="eclipselink.logging.level" value="FINEST"/>
</properties>

, jpql- sql.

( !): ?

,

+2

, ? , , ,

/**** 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);

, -

genre.getGenreBean().add(game);
em.persist(genre);

,

:

, -

select G, Game from Genre, IN(g.getGenreBean()) Game

Collection of Object [] 0, 1

+1

.

, , , , , null. . id 2 - , id 1, 1,

(thanks Chris Babich) :)

So, my significant lesson of the day: the results of checking for duplication in the database, see if you have the appropriate records. Use

    <properties>
      <property name="eclipselink.logging.level" value="FINEST"/>
   </properties>

When debugging

And I added insert functionality. So thanks for this Jorge.

Thanks for taking the time to respond and read.

So relationships work very well with JPA. ALOT is better than linq to sql in my opinion.

Now I can call the following:

game.getPublisherBean().getPublisher();

And I get the desired result. So, "RPG" instead of ""

+1
source

Source: https://habr.com/ru/post/1796038/


All Articles