Jan Bouchner
Jan Bouchner

Reputation: 897

Hibernate entity - add business logic to entity?

I have entity:

@Entity(name = "Term")
@Table(name = "extra_term")
public class Term implements Cloneable, Serializable{

This entity has ID

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

And this entity has attribute with List of users owned by this entity (users are registered to term)

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "etc. etc. etc. })    
private List<TermUser> users = new ArrayList<TermUser>();   

Is it possible to declare business method to count the sum of this users registered to concrete term? Something like

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?)"
}

I need to count the number of users registered to term straight from the entitz but I dont want to have a attribute "registeredUsersCount" in DB.

Upvotes: 1

Views: 881

Answers (1)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

That's exactly what @Transient is for.

Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient.

Upvotes: 2

Related Questions