I am building a menu in my application, so I have made entity MenuItem, which represents one item in my menu. This can be either a file or directory.
However, I want to know whether a directory has any children or not, because if it doesn't I do not want to display it. I also do not want to have this number of children hardcoded, because that would mean I would have to update the value everytime I add something.
What I want to know is, if there is a way to map an attribute with a query instead of a persisted value.
This is my MenuItem.java file:
@Entity
@Table(name = "menu_item")
public class MenuItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private long parent;
@Column
private String name;
@Column
private long num_of_childs;
@Enumerated(EnumType.STRING)
@Column
private MenuItemType type;
@ManyToOne
@JoinColumn(name = "file_id")
private FileItem file;
/* getters and setters... */
}
What I want is something like this:
@Column
@Transient
@Query("SELECT COUNT(i) as num_of_childs FROM MenuItem i WHERE parent = i.id")
private long num_of_childs;
Is it even possible to do such thing?