错误:RuntimeException:没有为name [default]配置JPA EntityManagerFactory

时间:2013-07-17 08:00:33

标签: playframework-2.0

我收到此错误:[RuntimeException:没有为name [default]配置JPA EntityManagerFactory]当我尝试在数据库中创建动作搜索时。

MODELS --- 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();
    }
}

CONTROLLER --- Application.java

public class Application extends Controller {

 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()

Application.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

1 个答案:

答案 0 :(得分:2)

为几个项目工作。在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(&#34;默认&#34;)而不是JPA.em()。