Java JPA一对多 [英] Java JPA one to many

查看:77
本文介绍了Java JPA一对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了有关填充JTable的信息,该JTable现在已完全可用. 不管怎样,我现在还停留在其他事情上,希望通过这个线程实现两个特定的发展.

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.

我有以下情况

两个表:

游戏
id pk int
类型int
标题varchar ....

games
id pk int
genre int
title varchar ....

类型
id pk int
名称varchar ..

genre
id pk int
name varchar ..

一种游戏只能具有一种类型,但是一种类型可以包含多种类型的游戏.

One game can only have one genre, but one genre can contain many games.

我正在使用Eclipse作为我的IDE,并使用eclipselink作为我的JPA提供程序.

I'm using Eclipse as my IDE and eclipselink as my JPA provider.

我已经使用eclipse提供的实体向导生成了类,他也允许我定义两个表之间的关系.

I have generated the classes with the entity wizard provided by eclipse, whom also allowed me to define relationships between the two tables.

关系如下:

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

我使用以下内容选择所有游戏

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.

如何配置列表以包含具有以下属性的游戏:ID,标题,流派名称,发行者名称,发布日期

How do I configure the list to contain a game that has the following attributes: id, title, genre-name, publisher-name, releasedate

在我的JTable中,我尝试了以下操作:

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();
    }

在这种情况下,在我的JTable中,情况1和2的列为空,而其余的起作用.

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.

请让我知道代码中是否存在任何错误或错误,或者选择查询,因为我什至找不到我是否必须编写显式的JOIN查询,并且由于定义而没有执行此操作关系.

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

谢谢您的时间.

推荐答案

为了进一步解决该问题,您可以通过在persistence.xml中(在persistence-unit元素内)添加以下内容来启用细粒度的JPA日志记录: /p>

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>

然后查看您的日志文件,并查看jpql查询如何转换为sql.

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?

希望这会有所帮助

这篇关于Java JPA一对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆