【问题标题】:How to save an ArrayList in Play?如何在 Play 中保存 ArrayList?
【发布时间】:2014-04-25 23:50:13
【问题描述】:

这似乎是一个非常基本的问题,但我有一个模型(用户),我想存储一个字符串数组列表(它们是其他用户的 id)。我这样声明列表:

 public List<String> friends = new ArrayList<String>();

在向数组添加条目后,我保存了用户。但是当我尝试使用它时,朋友总是为空。是否有特定的方法来保存 ArrayList?任何帮助将不胜感激。

我的模特:

@Entity
public class User extends Model {

@Id
public String username;
public String password;

public List<String> friends = new ArrayList<String>();

public static Finder<String, User> find = new Finder<String, User>(String.class, User.class);

// Constructor
public User(String username, String password){
    this.username = username;
    this.password = password;
}

// Methods
public void addFriend(String friend){
    friends.add(friend);
}

// Static Methods
public static User authenticate(String username, String password){
    return find.where().eq("username", username).eq("password", password).findUnique();
}

public static void befriend(String user1, String user2){
    User.find.ref(user1).addFriend(user2));
    User.find.ref(user2).addFriend(user1);

    User.find.ref(user1).save();
    User.find.ref(user2).save();
}

}

控制器方法:

return ok(index.render(
        User.find.byId(request().username()).friends,
));

还有一个非常简单的观点:

@(friends: List[User])

<div id="current_friends">
    @for(friend <- friends) {
        @friend.username
    }
</div>

【问题讨论】:

  • 您的 Arraylist 声明没有问题。您能否分享保存到数组列表中的代码以及您尝试检索的方式。
  • @JunedAhsan 我已经编辑了我的问题以显示更多代码。它的其他部分对您有帮助吗?

标签: java arraylist playframework-2.0 ebean


【解决方案1】:

您需要使用saveManyToManyAssociations(String fieldname)“手动”保存关系,例如:

public static void befriend(String userName1, String userName2){

    User user1 = User.find.byId(userName1);
    User user2 = User.find.byId(userName2);

    user1.friends.add(user2);
    user2.friends.add(user1);

    user1.save();
    user2.save();

    // here...
    user1.saveManyToManyAssociations("friends");
    user2.saveManyToManyAssociations("friends");

}

(注意:写在我的上面,所以请自己调试)

【讨论】:

    【解决方案2】:

    此问题的一个潜在原因可能是您的观点:

    你的视图的第一行是

    @(friends: List[User])
    

    用户没有包名,可能导致空指针异常。 在我的例子中,我的用户 bean 在模型包下,所以我有以下行:

    @(friends: List[models.User])
    

    【讨论】:

      【解决方案3】:

      我遇到了完全相同的问题,下面是我解决它的方法(附带一些解释)。

      实际上,您尝试将 ArrayList(因此大小未定义)保存在数据库中。显然(并且非常合乎逻辑),Play Framework 并不喜欢它;您必须使用注释或瞬态类。我决定用class的方式(也是因为我不知道怎么用注解做分表,所以没有冒险,但也不是最好的做法。其实是一个可怕的做法。但是,它仍然是)。

      在你的情况下,你可以这样做:

      @Entity
      public class Friends extends Model {
          @Id
          public Long id;
          @Required
          public String user1;
          @Required
          public String user2;
      
          public static Finder<Long, Friends> find = new Finder<Long, Friends>(Long.class, Friends.class);
      
          //Here put your functions, I myself only added an insert method for the moment :
      
          public static void add(String user1, String user2){
              Friends f = new Friends();
              f.user1 = user1;
              f.user2 = user2;
              bu.save();
          }
      }
      

      在您的用户模型中,只需通过此功能更改将两个用户保存到彼此列表中的部分。

      希望这会有所帮助。

      注意:id在这里是因为我喜欢数字id,请随意更改。

      注2:当然,使用@ManyToOne 和@OneToMany 注释会好很多,但正如我之前写的,我不知道它是如何工作的。

      【讨论】:

        猜你喜欢
        • 2014-05-17
        • 2016-07-29
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        • 2016-10-08
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多