Reputation: 23
I have a example JPA entity:
@Entity
@Table(name = "tb_group")
public class Group
{
...
@ManyToMany
@JoinTable(name = "tb_group_user",
joinColumns = @JoinColumn(name = "fk_group_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "fk_user_id", referencedColumnName = "id"))
private List<User> users;
private Integer userSize;
...
}
My question is, how can i initialize userSize field, with the size value of users field,that is a Lazy Load field?
I know its a dumb question, but i can't find a good strategy to solve this problem.
I tried this solution, but haven't succeed:
private Integer userSize = users.size();
I'm confused with this problem. Can you help me with an example?
EDIT:
I tried solution @Formula("select count(gu.fk_user_id) from tb_group_user gu where gu.fk_group_id = id")
suggested by Ady Junior, but i receive this exceptions, when i try get groups:
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax to use near
select count(gu.fk_user_id) from tb_group_user gu where gu.fk_group_id = group
ERROR br.com.loopec.loopkey.server.controller.ExceptionsHandler
Unhandled Exception: org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
EDIT 2:
I got it solved the problem. Ady Junior give me a good solution, and the error was due to my stupidity. Inside @Formule("select count(gu.fk_user_id) from tb_group_user gu where gu.fk_group_id = id")
i forgot putied parentheses '(' ')' between query.
The correct solution for my problem this:
@Entity
@Table(name = "tb_group")
public class Group
{
...
@ManyToMany
@JoinTable(name = "tb_group_user",
joinColumns = @JoinColumn(name = "fk_group_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "fk_user_id", referencedColumnName = "id"))
private List<User> users;
@Formula("(select count(gu.fk_user_id) from tb_group_user gu where gu.fk_group_id = id)")
private Integer userSize;
...
}
Thanks Ady Junior and Thanks Christian Beikov
Upvotes: 1
Views: 410
Reputation: 1080
devsaleh, try use @Formula to write a count query.
There are many features about this annotation. For instance, in this awesome post written by Vlad Mihalcea: https://vladmihalcea.com/how-to-map-calculated-properties-with-jpa-and-hibernate-formula-annotation/
@Entity
@Table(name = "tb_group")
public class Group
{
...
@ManyToMany
@JoinTable(name = "tb_group_user",
joinColumns = @JoinColumn(name = "fk_group_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "fk_user_id", referencedColumnName = "id"))
private List<User> users;
@Formula("(select count(gu.fk_user_id) from tb_group_user gu where gu.fk_group_id = id)")
private Integer userSize;
...
}
@devsaleh, Thank you very much!
Best Regards!
Upvotes: 1
Reputation: 16430
You could use extra-lazy collections with @LazyCollection(LazyCollectionOption.EXTRA)
but I wouldn't recommend that: https://vladmihalcea.com/hibernate-extra-lazy-collections/
The approach Ady Junior proposed i.e. to use @Formula("select count(gu.fk_user_id) from tb_group_user gu where gu.fk_group_id = id")
is a way you could go, but probably it's better to use a DTO query and determine the size in the query you use to load the data. Something like this
entityManager.createQuery("SELECT g.name, SIZE(g.users) FROM Group g")
Upvotes: 1