我有实体:
@Entity(name = "Term")
@Table(name = "extra_term")
public class Term implements Cloneable, Serializable{
此实体有 ID
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
并且该实体具有该实体拥有的用户列表的属性(用户已注册到任期)
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "etc. etc. etc. })
private List<TermUser> users = new ArrayList<TermUser>();
是否可以声明业务方法来计算该用户注册到具体期限的总和?就像是
public long getUsersCount() {
return QUERY TO THE DATABASE "GET COUNT OF USERS REGISTERED TO TERM WITH ID
(where ID is the id of this entity?)"
}
我需要直接从 entitz 计算注册到期限的用户数量,但我不想在 DB 中有一个属性“registeredUsersCount”。