简体   繁体   中英

How to initialize fields with size others fields in entity JPA

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

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 ie 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")

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM