my question is exactly the same as this in hibernate but minus the ordering.
I have two tables, Person
and Fruit
@Entity
@Table(name = "person")
public class Person {
@Id
@Setter
@Getter
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
@Getter
@Setter
private String name;
@Getter
@Setter
@Column(name = "fruit_ids")
private Integer[] fruitIds;
}
Table "public.person”
Column | Type | Modifiers | Storage | Stats target | Description
---------------------+--------------------------------+---------------------------------------------------+----------+--------------+------------------------------------------
id | integer | not null default nextval(‘person_seq'::regclass) | plain | |
name | character varying(255) | | extended | |
fruit_ids | integer[] | | extended | |
and fruit
@Entity
@Table(name = “fruit”)
public class Fruit {
@Id
@Setter
@Getter
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
@Getter
@Setter
private String name;
}
Table "public.fruit”
Column | Type | Modifiers | Storage | Stats target | Description
---------------------+--------------------------------+---------------------------------------------------+----------+--------------+------------------------------------------
id | integer | not null default nextval(‘fruit_seq'::regclass) | plain | |
name | character varying(255) | | extended | |
What I want to approach is something like select p from person p join fruit f on f.id = any(p.fruit_ids) where p.id = 5;
How can I do it in Hibernate? I currently have
@Getter
@Setter
@OneToMany
@JoinColumn(name = "fruit_ids", referencedColumnName = "id")
private List<Fruit> Fruits= new ArrayList<>();
but it gives me error. Please note that fruit_ids
in person
table is an array of integer
. This is not the old school one to many relationship in mysql.