void
void

Reputation: 35

Hibernate change property instance by id

Say we have a User class with UserType property defined:

private UserType userType;

Is it possible to set this property using only ID of needed UserType? Something like

public void setUserTypeById(Long id){
   this.userType.setId(id); 
}

But this one will only change the id property of assigned UserType instance, while I need to change it to another one.

Upvotes: 2

Views: 89

Answers (1)

rolve
rolve

Reputation: 10218

In theory, it would be possible, if you had access to a session or to a DAO in your User class. Then you could just retrieve then UserType corresponding to the given ID and set it.

But I do not recommend this. I think it is better to clearly separate the domain classes (User, UserType) from the persistence classes (DAOs or session). Better create a service class (UserService for instance) that has a method setUserType(User user, int typeId) which takes care of this.

Upvotes: 2

Related Questions