I have 2 entities User and UserSettings. I am trying to build a flow to modify UserSettings row in the DB. this is my User and UserSetting object-
@Entity
@Table(name = "user")
public class User implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private int id;
private String name;
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime createdat = new DateTime();
@OneToMany( fetch=FetchType.EAGER)
@JoinColumn(referencedColumnName="id", name = "userID")
@BatchSize(size = 50)
@MapKey(name="name")
@Cascade({CascadeType.ALL})
private Map<String,UserSetting> userSetting;
...
}
@Entity
@IdClass(UserSetting.UserSettingId.class)
@Table(name = "usersettings")
public class UserSetting implements Serializable{
@Id
private String name;
private String value;
@Column(name="UserID",insertable = false,updatable = false) //
private Integer userId;
@Id
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="userID")
private User user;
...
}
before modifying the UserSetting I am trying to fetch the object, modifying its value, and then using the save method.
this is the method for saving the modified data -
public void saveUserSettings(int userId, String name, String value) {
UserSetting userSetting = new UserSetting();
userSetting.setUserId(userId);
userSetting.setName(name);
userSetting.setValue(value);
User user = usersRepository.findOne(userId);
userSetting.setUser(user);
userSettingRepository.save(userSetting);
}
the save always fails, because the user that returns from usersRepository.findOne(userId) is a lazy object and not the actual one.
Why does this happen? How can I make the find one return the actual object?
thanks ahead
Yair