upog
upog

Reputation: 5531

Hibernate mapping on non-unique column one to Many Mapping

I have two tables

Person
   id --> PK
   name
   media_id

Media
  id --> PK
  media_id
  other_details

The Media.Media_id will have duplicate values

now I want to load all media records into person having the given media id

public class Person{

    @Id
    private id;

    private String name;

    @OneToMany  (**Want to map using MEDIA ID**)
    private List<Media> media= new ArrayList<>();

}

Person to Media is unidirectional, Want to know how to do this mapping

Upvotes: 0

Views: 174

Answers (1)

Rasha Elsayed
Rasha Elsayed

Reputation: 740

This is not a oneToMany Relationship. You can use @Formula, like this

public class Person{

@Id
private id;

private String name;

private int media_Id // Add a mediaId field to refer to it in the formula

@Transient
@Formula("SELECT * FROM MEDIA WHERE MEDIA_ID = media_Id")
private List<Media> media= new ArrayList<>();

}

Upvotes: 1

Related Questions