Hibernate relacja one to many.

0

Cześć.

jest encja:

Entity
@Table(name = "groups")
public class Group implements Serializable {

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

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

do tego

@Entity
@Table(name = "students")
public class Student implements Serializable {

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

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

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id")
    private Group group;

mam GroupDao , da się stworzyć kryteriami hibernate'owymi jedno zapytanie, które zwróci mi grupy bez żadnego studenta?
tzn. jeżeli nie ma do tej grupy ani jednej referencji z tableki students. czy zostaje mi tylko Java i wykorzystanie StudentDao

0

Równoważne `where group_id not in (select distinct group_id from students)'?

Da się za pomocą podzapytań http://stackoverflow.com/questions/3738555/hibernate-criteria-subquery
oraz restrykcji not i in http://stackoverflow.com/questions/1220901/how-to-achieve-not-in-by-using-restrictions-and-criteria-in-hibernate

0

Na początku dodaj odwrotną relację w group, tj @OneToMany ze studentami.
Potem możesz zrobic cos z stylu:

SELECT e FROM Employee e WHERE e.projects IS EMPTY

Powyższy cytat jest z książki Pro JPA2. Polecam.

1 użytkowników online, w tym zalogowanych: 0, gości: 1