我想更新/编辑我的数据库信息。但是当我试图这样做时,它显示错误:
执行例外
发生PersistenceException: org.hibernate.PersistentObjectException:传递给的分离实体 坚持:models.User
我的模特是:
@Entity
public class User extends Model implements RoleHolder {
@Required
@Unique
@MaxSize(120)
@Email
public String email;
@Required
@Unique
@MaxSize(15)
@MinSize(4)
@Match(value = "^\\w*$", message = "Not a valid username")
public String name;
@Required
@MaxSize(15)
@MinSize(4)
@Match(value = "[^=]*", message = "Not a valid password")
@Password
public String password;
@Transient
@Equals(value = "password", message = "Password doesn't match")
@Password
public String confirmPassword;
@MaxSize(100)
public String displayName;
@Required
@ManyToOne
public Role role;
@Required
@ManyToOne
public SchoolInformation school;
@Required
@ManyToOne
public Ngo ngo;
/* GEO-location Hierarchy */
@ManyToOne
public GeoDivision geoDivision;
@ManyToOne
public GeoDistrict geoDistrict;
@ManyToOne
public GeoUpazilla geoUpazilla;
/**
* @param email
* @param password
*/
public User(String email, String password) {
this.email = email;
this.password = password;
/* this.geoPSUs = new TreeSet<GeoPSU>(); */
}
/**
* @param email
* @param password
* @param name
*/
public User(String email, String password, String name) {
this(email, password);
this.name = name;
}
public User(String email, String password, String name, Role role) {
this(email, password, name);
this.role = role;
}
// From RoleHolder Interface
@Override
public List<? extends Role> getRoles() {
List<Role> list = new ArrayList<Role>();
list.add(this.role);
return list;
}
/**
* Authenticate.
*
* @param username
* the username
* @param password
* the password
* @return the user
*/
public static User authenticate(String username, String password) {
return User.find("byNameAndPassword", username, Crypto.passwordHash(password, HashType.SHA512)).first();
}
/**
* Find User by name
*/
public static User findByName(String name) {
return User.find("byName", name).first();
}
/**
* Before save a User
*/
@PrePersist
@PreUpdate
public void beforeSave() {
updatePassword();
}
/**
* Update password by its hash value
*/
private void updatePassword() {
Logger.info("password set for " + this);
this.password = Crypto.passwordHash(this.password, HashType.SHA512);
}
/**
* Return list of User with given role
*/
public static List<User> getRoleUser(Role role) {
return User.find("role = ?", role).fetch();
}
@Override
public String toString() {
return this.id + " " + this.name + " " + this.email + " ";
}
public static User findByLogin(String username) {
return User.find("byName", username).first();
}
public static List<User> findFsByDsUser(long dsId) {
return User.find("byDs_id", dsId).fetch();
}
public static List<User> findEnumeratorByFsUser(long fsId) {
return User.find("byFs_id", fsId).fetch();
}
public static List<User> findAllEnumeratorUsers() {
return User.find("byRole_id", 4).fetch();
}
public boolean isAdmin() {
return role.equals(Role.getAdminRole());
}
public boolean isEnumerator() {
return role.equals(Role.getEnumeratorRole());
}
/**
* Return parent User. From Edmis hierarchy, M&H for M&C, M&C for DM, DM for
* FF, otherwise null
*/
public User getParentUser() {
User parentUser = null;
return parentUser;
}
public static List<User> getChildEnumerators(User loggedInUser) {
List<User> enumList = new ArrayList<User>();
List<User> users = User.findAll();
for (User user : users) {
if (user.getParentUser().equals(loggedInUser)
|| user.getParentUser().getParentUser().equals(loggedInUser)) {
enumList.add(user);
}
}
return enumList;
}
public void update(Long id) {
String qString = null ;
Connection conn = play.db.DB.getConnection();
ResultSet rs = null;
}
}
我的控制器是:
@ExternalRestrictions("Edit User")
public static void edit(Long id) {
User user = User.findById(id);
flash("user", "" + user.id);
notFoundIfNull(user, "user not found");
user.password = null;
List<Role> roles = Role.findAll();
List<SchoolInformation> schoolList = SchoolInformation.find("approavedStatus = ? ", "Approved").fetch();
List<Ngo> ngoList = Ngo.findAll();
List<GeoDivision> geoDivisionList = GeoDivision.findAll();
List<GeoDistrict> geoDistrictList = GeoDistrict.findAll();
List<GeoUpazilla> geoUpazillaList = GeoUpazilla.findAll();
render(user,schoolList,ngoList, roles,geoDivisionList,geoDistrictList,geoUpazillaList);
}
@ExternalRestrictions("Edit User")
public static void submit(@Valid User user) {
Logger.info("flashUserId in Submit: " + flash.get("user"));
validation.valid(user);
if(validation.hasErrors() && flash.get("user") == null) {
List<Role> roles = Role.findAll();
Logger.info("hasError");
render("@edit", user, roles);
}
if(flash.get("user") != null){
User editedUser = User.findById(Long.parseLong(flash.get("user")));
editedUser.name = user.name;
editedUser.id = user.id;
//Logger.info("updated username : " + editedUser.name);
editedUser.displayName = user.displayName;
editedUser.email = user.email;
editedUser.role.id = user.role.id;
editedUser.school.id = user.school.id;
editedUser.ngo.id = user.ngo.id;
editedUser.geoDivision.id = user.geoDivision.id;
editedUser.geoDistrict.id = user.geoDistrict.id;
editedUser.geoUpazilla.id = user.geoUpazilla.id;
Logger.info("name: " + editedUser.name + " displayname: " + editedUser.displayName + " email: " + editedUser.email + " role: " + editedUser.role.id + " ngoId: " + editedUser.ngo.id + " geoDivisionId: "
+ editedUser.geoDivision.id + " geoDistrictId: "+ editedUser.geoDistrict.id + " geoUpazilaId: "+ editedUser.geoUpazilla.id);
editedUser.save();
}
else {
user.save();
create();
}
flash.success("Record saved successfully.");
list("0");
}
我必须更改ID因为,我想更新表的信息。假设,这是当前的条件:
然后,我想像这样更新Upazilla:
然后显示错误。
问题在哪里?答案 0 :(得分:0)
我通过改变我的控制器解决了这个问题:
@ExternalRestrictions("Edit User")
public static void submit(@Valid models.User user) {
Logger.info("flashUserId in Submit: " + flash.get("user") + " and user id is : " + user.id);
validation.valid(user);
if(validation.hasErrors() && flash.get("user") == null) {
List<Role> roles = Role.findAll();
Logger.info("hasError");
render("@edit", user, roles);
}
Logger.info ("id: "+ user.geoUpazilla.id);
if(flash.get("user") != null){
User editedUser = User.findById(Long.parseLong(flash.get("user")));
editedUser.name = user.name;
editedUser.displayName = user.displayName;
editedUser.email = user.email;
editedUser.role = user.role;
editedUser.school= user.school;
editedUser.ngo = user.ngo;
editedUser.geoDivision = user.geoDivision;
editedUser.geoDistrict = user.geoDistrict;
editedUser.geoUpazilla = user.geoUpazilla;
Logger.info("id : " + editedUser.id + " name: " + editedUser.name + " displayname: " + editedUser.displayName + " email: " + editedUser.email + " role: " + editedUser.role.id + " ngoId: " + editedUser.ngo.id + " geoDivisionId: "
+ editedUser.geoDivision.id + " geoDistrictId: "+ editedUser.geoDistrict.id + " geoUpazilaId: "+ editedUser.geoUpazilla.id);
editedUser.save();
}
else {
user.save();
create();
}
flash.success("Record saved successfully.");
list("0");
}