2

我收到此错误:[RuntimeException: No JPA EntityManagerFactory configured for name [default]] 当我尝试在数据库中创建操作搜索时。

模型 --- user.java

@Entity
public class User extends Model{

    @Required
    public String MaKH;

    @Id
    public Integer Id;

    @MinLength(6)
    public String Ten;

    public String Diachi;

    public Integer SDT;

    public User() {}

    public User(String MaKH,String Ten,String Diachi,Integer SDT,Integer Id){
        this.Diachi=Diachi;
        this.MaKH=MaKH;
        this.Ten=Ten;
        this.SDT=SDT;
        this.Id=Id;
    }


    // Find method static for request
    public static Finder<Long,User> find = new Finder(Long.class,User.class);
    public static List<User> searchByName(String name){
        return find.where().like("Ten", "%"+name+"%").findList();
    }
}

控制器---Application.java

公共类应用程序扩展控制器{

 public static Result index() {
     return ok(index.render());
 }
 @Transactional
 public static Result search() {
            DynamicForm form = form().bindFromRequest();
            String name = form.get("Ten");
            User.searchByName(name);
      return ok(result.render());

} }

路线

GET      /                           controllers.Application.index()
POST     /search                 controllers.Application.search()

应用程序.conf

 db.default.driver=com.mysql.jdbc.Driver
 db.default.url="jdbc:mysql://localhost/search"
 db.default.user=root
 db.default.password=123456
 db.default.jndiName=DefaultDS
 ebean.default="models.*"

请帮我解决它:D

4

1 回答 1

3

为几个项目工作。在 application.conf 添加

jpa.default=defaultPersistenceUnit

喜欢

...
# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3305/dbname"
db.default.user=root
db.default.password=""
#db.default.maxConnectionsPerPartition=5
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

# Evolutions...

另外,在 sbt for play 2.2.3 我添加了

libraryDependencies ++= Seq(
  javaJdbc,
  javaJpa,
  cache
)

play.Project.playJavaSettings

val appDependencies = Seq(
  javaJdbc,
  javaJpa,
  "org.hibernate" % "hibernate-entitymanager" % "4.3.5.Final",
  "mysql" % "mysql-connector-java" % "5.1.29"
)

ebeanEnabled in Global := false

也许如果persistence.xml、类、表和java注释一切正常,你清理所有东西并重新启动并在任何地方添加@Transactional,你几乎不会出错。此外,有时使用 JPA.em("default") 代替 JPA.em()。

于 2014-06-19T09:56:47.527 回答