Auto-relacionamento JPA

E ai pessoal,

estou mapeando uma classe que tem um auto relacionamento em JPA…
está acontecendo o seguinte problema:

quando tento pegar as classes filhas com getSubs() ele retorna todas as classes que não tem pai, ao invés de retornar
as classes filhas do objeto em questão

código da classe

[code]@Entity(name = “MONITORAMENTO”)
public class Monitoring implements Serializable {

private static final long serialVersionUID = 1L;

public static final int   DAILY            = 1;
public static final int   WEEKLY           = 2;
public static final int   MONTHLY          = 3;

@Id
@SequenceGenerator(name = "SQ_MONITORAMENTO", sequenceName = "SQ_MONITORAMENTO", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_MONITORAMENTO")
@Column(name = "CD_MONITORAMENTO", nullable = false)
private long              id;

@Column(name = "CD_MONITORAMENTO_PAI", nullable = true)
private long              parentId;

@Column(name = "NO_MONITORAMENTO", nullable = false)
private String            name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CD_MONITORAMENTO_PAI", referencedColumnName = "CD_MONITORAMENTO_PAI", updatable = false, insertable = false)
private Monitoring        parent;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
private List<Monitoring>  subs;

}[/code]

aangreen,

Observe que vc não precisa disso:

    @Column(name = "CD_MONITORAMENTO_PAI", nullable = true)
    private long              parentId;

e precisa definir em sua anotação ManyToOne desta maneira:

@JoinColumn(name = "CD_MONITORAMENTO_PAI", referencedColumnName = "CD_MONITORAMENTO", updatable = false, insertable = false)

No final deveria ficar assim sua classe:

@Entity(name = "MONITORAMENTO")
public class Monitoring implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int   DAILY            = 1;
    public static final int   WEEKLY           = 2;
    public static final int   MONTHLY          = 3;

    @Id
    @SequenceGenerator(name = "SQ_MONITORAMENTO", sequenceName = "SQ_MONITORAMENTO", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_MONITORAMENTO")
    @Column(name = "CD_MONITORAMENTO", nullable = false)
    private long id;

    @Column(name = "NO_MONITORAMENTO", nullable = false)
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CD_MONITORAMENTO_PAI", referencedColumnName = "CD_MONITORAMENTO", updatable = false, insertable = false)
    private Monitoring parent;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
    private List<Monitoring>  subs;

    .......
    .......
    .......
    .......
}

Espero que te ajude.

@braços,
Cleiton

Ajudou sim cara…

valew ai… Problema resolvido