How to get a null element in HQL?

I have Entity and NamedQuery:

@Entity @Table(name="MY_TABLE") @NamedQueries({ @NamedQuery(name="myQuery", query="select m from MyEntity m where m.child.x = 7" }) public class MyClass { @Column(name="CHILD_COL") private Child child; // getter and setter } public class Child { int x; // getter and setter } 

Now I want to change it to have a list o Child:

 @Entity @Table(name="MY_TABLE") @NamedQueries({ @NamedQuery(name="myQuery", query="select m from MyEntity m where m.childs[0].x = 7" }) public class MyClass { @Column(name="CHILD_COL") private List<Child> childs; // getter and setter } 

But the syntax of 'childs [0] .x' does not exist. Any idea how I can do this?

+6
source share
1 answer

Indexed lists are supporterd hibernate, but definition is important.

In the old way (XML mapping) you can work as follows:

In pojo:

 private List<Child> childs; 

In the XML mapping:

 <list name="childs" table="yourtable" cascade="all,delete-orphan" inverse="false" lazy="false"> <key column="fk_to_parent"/> <list-index column="an_integer_column"/> <one-to-many class="Child" /> </list> 

In a JPA annotation, you should use an IndexColumn annotation as follows:

 @IndexColumn(name="an_integer_column", base=0, nullable=false) 

So you will have:

 @Column(name="CHILD_COL") @IndexColumn(name="an_integer_column", base=0, nullable=false) private List<Child> childs; 

Tell me is it good

0
source

Source: https://habr.com/ru/post/1013804/


All Articles