If you don't want UserBanned
to have reference to User
, you could make the UserBanned
embeddable and embed it in User
.
@Embeddable
public class UserBanned{
....
}
@Entity
@Table(name = "user")
@DynamicInsert
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "user_id_generator")
@SequenceGenerator(name = "user_id_generator", sequenceName = "user_id_seq")
protected Integer id;
@Column
protected Integer name;
@Embedded
protected UserBanned userBanned;
If you want the UserBanned
to be in separate database-table and the table should contain reference to the user
-table, I don't think you can avoid having the User
in UserBanned
, otherwise Hibernate won't know which User
the UserBanned
refers to.
If you want just to get rid of the line userBanned.setUser(user);
, you could set the User
to UserBanned
in Users
setUserBanned(userBanned );
-method.
public void setUserBanned(UserBanned userBanned) {
this.userBanned = userBanned;
userBanned.user(this);
}
And then excluding the field from serialization for example in way suggested in Ilyas answer.