开发者

Left join bringing 2 different objects

I could n开发者_StackOverflowot make a better title, if anyone can do it please, help me out! Same for tags.

I've made a JPQL to bring me one Object with a List of other Objects.

The thing that's happening is.

I've got 2 items in Novidade(DB).

I've got 2 items in ComentarioNovidade(DB).

1 of the items from Novidade, connects to all 2 items from ComentarioNovidade. The other has no ComentarioNovidade related.

JPQL returns a List of Novidade (it's supposed to be)

I'm trying to make it return one Novidade with all ComentarioNovidade in it if it has any.

It's returning 3 Objects containing Novidade and ComentarioNovidade separated.

Left join bringing 2 different objects

My JPQL is like this:

from Novidade as n left outer join n.comentariosNovidade

The class Novidade:

@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CodNovidade")
    private Integer codNovidade;

    @Column(name="Mensagem")
    private String mensagem;

    @Column(name="CodigoCidade")
    private int codCidade;

    @Column(name="CodigoBairro")
    private int codBairro;

    @Column(name="MesmoBairro")
    private String mesmoBairro;

    @OneToMany
    @JoinColumn(name="CodNovidade")
    private List<ComentarioNovidade> comentariosNovidade;

The class ComentarioNovidade:

@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CodComentarioNovidade")
    private Integer codComentarioNovidade;

    @Column(name="Comentario")
    private String comentario;

    @ManyToOne
    @JoinColumn(name="CodNovidade")
    private Novidade novidade;

    @ManyToOne
    @JoinColumn(name="CodUsuario")
    private Usuario usuario;


A friend helped me out with that.

My JPQL ended up like this:

select distinct n from Novidade as n left outer join fetch n.comentariosNovidade
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜