如何将数据库条目加载到我的scala模板中?

时间:2015-06-02 12:41:22

标签: database scala playframework

我正在使用游戏2.3.8并构建一个程序,您可以在其中提出问题并回答它们。我有几个不同的用户,所以我使用他们唯一的电子邮件作为数据库中的ID。

问题/答案有一个ownerID,用于显示谁编写了它们。

我的用户存储在数据库中,如下所示:

Email           Name Password
bob@mail.com    Bob  secret

我的问题存储在数据库中,如下所示:

QUESTION_ID                             QUESTION_TEXT       VOTE_SCORE      OWNER_ID        PAGE  
77b7f88a-41df-4d68-9f89-de508fce8f71    How tall is tall?   1228            bob@mail.com    1

我的控制器类,它从数据库中收集问题/答案并将列表发送到视图类:

public static List<Question> questionListAll = new ArrayList<Question>();
public static List<Answer> answerListAll = new ArrayList<Answer>();

public static Result index() {
        questionListAll.clear();
        answerListAll.clear();

        // Get all questions from DB
        for (Question questionItem : Question.find.all()) {
            questionListAll.add(questionItem);
        }

        // Get all answers from DB
        for (Answer answerItem : Answer.find.all()) {
            answerListAll.add(answerItem);
        }

        Collections.sort(questionListAll, Collections.reverseOrder());
        Collections.sort(answerListAll, Collections.reverseOrder());

        return ok(views.html.index.render(questionListAll, answerListAll));
    }

User.java:

// FIXME Dont really save the password as String...
@Id
public String email;
public String name;
public String password;
(...)

Question.java:

@Entity
public class Question extends Model implements Comparable<Question> {
    @Id
    public String questionID;
    public String questionText;
    public Integer voteScore;
    public String ownerID;
    public Integer page;
(...)
}

在我的视图类中,我使用@answer.ownerID显示问题并在其后面显示用户...但是当我使用电子邮件字段作为ID时,条目类似于:

"How tall is tall?" - bob@mail.com

我想要的是:

"How tall is tall?" - Bob Ross(使用他们的名字,而不是他们的电子邮件)

我知道我可以在我的控制器中找到所有问题及其所有者,并将用户放入另一个列表中,我将使用@users.username,但这意味着我总是要查找所有用户,放他们进入名单,......

那么可以查看数据库,使用ownerID并在视图类中获取用户名吗?

或者更一般:您是否可以从视图类本身查找视图类的DB条目?

1 个答案:

答案 0 :(得分:1)

您可以使用JPA 2.0添加映射以在视图中实现所需的内容,然后您可以使用@question.owner.name@answer.owner.name

  

User.java

[...]
@Id
@Column(name="user_id")
public Long userId;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", cascade = CascadeType.ALL)
public List<Question> questionList;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", cascade = CascadeType.ALL)
public List<Answer> answerList;
[...]
  

Question.java

[...]
@Id
@Column(name="question_id")
public Long questionId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="user_id", nullable = false)
public User owner;

// @OneToMany: ORM join relationship, one Question to many Answers
// FetchType.LAZY: ORM will not fetch from db until you use it
// CascadeType.ALL: any changes to question will be propagated
//   onQuestionDelete: all answers associated with the question will be deleted
//   onQuestionUpdateAnswerList: any updates made will reflect on db
// mappedBy: Question object variable name on Answer object for ORM to make a connection
@OneToMany(fetch = FetchType.LAZY, mappedBy = "question", cascade = CascadeType.ALL)
public List<Answer> answerList;
[...]
  

Answer.java

[...]
@Id
@Column(name="answer_id")
public Long answerId;

// FetchType.EAGER: ORM will fetch this from db when Answer is fetch
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="user_id", nullable = false)
public User owner;

// @ManyToOne: ORM join relationship, many Question to one Answer
// @JoinColumn: name, pk column name of Question; nullable false, required constraint;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="question_id", nullable = false)
public Question question;
[...]