尝试为当前用户设置新的详细信息

时间:2013-12-12 11:58:45

标签: java

我对编程很新,而且我对java的了解有限。我正在修改一个像网站的Facebook模板,所以我可以学习和添加功能。我试图允许当前用户更改他们的详细信息,例如姓名,电子邮件,密码等,但是无法弄清楚java会这样做。当我尝试时,所有发生的事情都是在当前的用户之上创建一个全新的用户:/ 这是我到目前为止所做的:

public static void changeDetails(String firstName, String lastName, String email, String password, String age, String nationality)
  {
    Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

    User user = new User(firstName, lastName, email, password, age, nationality);
    user.save();
    Home.index();
  }

这是用户类:

package models;

import java.util.List;
import java.util.ArrayList;

import javax.persistence.OneToMany;
import javax.persistence.Entity;

import controllers.Home;
import play.db.jpa.Model;
import play.db.jpa.Blob;

@Entity
public class User extends Model
{
  public String firstName;
  public String lastName;
  public String email;
  public String password;
  public String age;
  public String nationality;
  public String statusText;
  public Blob   profilePicture;

  @OneToMany(mappedBy = "sourceUser")
  public List<Friendship> friendships = new ArrayList<Friendship>();

  public User(String firstName, String lastName, String email, String password, String age, String nationality)
  {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.password = password;
    this.age = age;
    this.nationality = nationality;
  }


  public static User findByEmail(String email)
  {
    return find("email", email).first();
  }

  public boolean checkPassword(String password)
  {
    return this.password.equals(password);
  }

  public void befriend(User friend)
  {
    Friendship friendship = new Friendship(this, friend);
    friendships.add(friendship);
    friendship.save();
    save();
  }

  public void unfriend(User friend)
  {
    Friendship thisFriendship = null;

    for (Friendship friendship:friendships)
    {
      if (friendship.targetUser== friend)
      {
        thisFriendship = friendship;
      }
    }
    friendships.remove(thisFriendship);
    thisFriendship.delete();
    save();
  }

  @OneToMany(mappedBy = "to")
  public List<Message> inbox = new ArrayList<Message>();

  @OneToMany(mappedBy = "from")
  public List<Message> outbox = new ArrayList<Message>();

  public void sendMessage (User to, String messageText)
  {
    Message message = new Message (this, to, messageText);
    outbox.add(message);
    to.inbox.add(message);
    message.save();
  } 



}

我可以看到我的问题可能是“新用户和用户。”但我无法弄清楚我应该拥有什么。感谢您的帮助:))

2 个答案:

答案 0 :(得分:1)

您必须将User类的实例传递给changeDetails方法,并使用setter方法更改它的属性。

编辑:

为您的代码添加了评论以查看错误:

public static void changeDetails(String firstName, String lastName, String email, String password, String age, String nationality)
  {
    Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

    //here you are creating new user object and setting it`s properties, wrong
    User user = new User(firstName, lastName, email, password, age, nationality); 
    user.save();
    Home.index();
  }

建议修复(假设您在User类中有setter方法):

public static void changeDetails(User user, String firstName, String lastName, String email, String password, String age, String nationality)
  {
    Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

    user.setfirstName(firstName);
    user.setlastName(lastName);
    user.setEmail(email); 
    user.setPassword(password); 
    user.setAge(age); 
    user.setNationality(nationality);

    Home.index();
  }

或者在看到您的编辑和更多没有setter方法的代码后,请执行以下操作:

public static void changeDetails(User user, String firstName, String lastName, String email, String password, String age, String nationality)
  {
    Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");

    user.firstName = firstName;
    user.lastName = lastName;
    user.email = email; 
    user.password = password; 
    user.age = age; 
    user.nationality = nationality;

    Home.index();
  }

答案 1 :(得分:0)

Althogh不清楚你的user.save()方法的实现。 有一点很清楚你的猜测是正确的。这是因为新的User()为您创建了一个新用户。

检查您的用户类是否有任何属性可以唯一标识用户(例如id)。 当您更改详细信息时,会传递该唯一ID属性以及其他详细信息,这些详细信息将说明需要更新哪些用户的详细信息。

再次,如果您使用任何持久性api,如hibernate,那么设置唯一ID属性就足以更新细节,否则您必须首先编写自定义代码,获取具有唯一ID的用户的详细信息,然后更新详细信息

再次,您的代码中没有任何属性的主键声明,我在User类中找不到save方法。我假设您正在使用休眠。

根据User类的代码,我假设电子邮件属性是您正在使用的唯一ID,因此请更改您的changeDetails方法

public static void changeDetails(String firstName, String lastName, String email, String password, String age, String nationality)
  {
    Logger.info("New details are: " + firstName + " " + lastName + " " + email + " " + password + " " + age + " " + nationality + " ");
    User user = findByEmail(String email);
    //User user = new User(firstName, lastName, email, password, age, nationality);
    user.setFirstName(firstName);
    user.setLastName(lastName);
    //user.setEmail(email);
    user.setPassword(password);
    user.setAge(age);
    user.setNationality(nationality);
    user.save();
    Home.index();
  }

正如您所看到的,我已经评论了user.setEmail(电子邮件),因为它会说它是一个新用户并将其保存为新用户。