【问题标题】:playframework JPA Error and DB design issueplayframework JPA 错误和数据库设计问题
【发布时间】:2026-01-06 19:25:01
【问题描述】:

我收到以下错误:

JPA 错误 发生 JPA 错误(无法构建 EntityManagerFactory):models.Issue.project 上的 @OneToOne 或 @ManyToOne 引用了未知实体:models.Project

在这里你可以看到我的实体:

package models;


import java.util.*;

import javax.persistence.*;
import play.db.jpa.*;
import models.Issue;
import models.Component;

public class Project extends Model{

public String self;
@Id
public String key;

@OneToMany (mappedBy="Project", cascade=CascadeType.ALL)
public List<Component> components;

@OneToMany (mappedBy="Project", cascade=CascadeType.ALL)
public List<Issue> issues;


public Project(String self, String key) {

    this.self = self;
    this.key = key;
    this.components = new ArrayList<Component>();
    this.issues = new ArrayList<Issue>();
}


public Project addComponent(String self, int component_id, String name, int issuecount) {

    Component newComponent = new Component(self, component_id, name, issuecount, this);
    this.components.add(newComponent);

    return this;
}


public Project addIssue(Date created, Date updated, String self, String key,
         String type, Status status) {

        Issue newIssue = new Issue(created, updated, self, key, type, status,  this);
        this.issues.add(newIssue);

        return this;
    }


}

这是另一个

package models;


import java.util.*;

import javax.persistence.*;
import play.db.jpa.*;

import models.Project;
import models.Status;
import models.Component;




@Entity
public class Issue extends Model {


@Id
public String key;
public Date created;
public Date updated;
public String self;
public String type;

@ManyToOne
public Status status;

@ManyToOne
public Project project;

@OneToMany
public List<Component> components;

public Issue(Date created, Date updated, String self, String key,
         String type, Status status,  Project project ) {

        this.created = created;
        this.updated = updated;
        this.self = self;
        this.key = key;
        this.status = status;
        this.type = type;
        this.project=project;
        this.components=new ArrayList<Component>();

}



public Issue addComponent(Component component) {

    this.components.add(component);

    return this;
}



}

我正在使用 Play 1.2.4 和 Eclipse。现在我的数据库在内存中。

我还有第二个问题。理想情况下,我需要为每个用户创建一个数据库,并且我想在每次用户登录(或注销)时删除表的内容,并在用户登录时再次填充表(这是因为存储在我的数据库中的信息必须与我正在连接的服务保持同步)。我该怎么办?

我完全不知道。请帮帮我。

【问题讨论】:

    标签: jpa playframework


    【解决方案1】:
    public class Project extends Model
    

    缺少@Entity 注释

    【讨论】:

    • @GLF 不要抱歉。这毕竟是一个问答网站。所以你做的一切都是正确的,也许其他人也可以从中学习;-)
    【解决方案2】:

    “mappedBy”应该引用另一个实体中的属性,即“project”而不是“Project”。

    【讨论】: