Mèo Béo
Mèo Béo

Reputation: 111

Error : RuntimeException: No JPA EntityManagerFactory configured for name [default]

i got this error : [RuntimeException: No JPA EntityManagerFactory configured for name [default]] when i try create action search in database.

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

} }

ROUTES

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.*"

Please help me solved it :D

Upvotes: 2

Views: 2807

Answers (1)

Ivan  Ivanov
Ivan Ivanov

Reputation: 2106

Worked for couple of projects. In application.conf add

jpa.default=defaultPersistenceUnit

like

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

Also, in sbt for play 2.2.3 I added

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

and maybe if everything is ok with persistence.xml and classes and tables and java annotations and you clean everything and restart and added @Transactional everywhere you could almost get no errors. Also, sometimes use JPA.em("default") instead of JPA.em().

Upvotes: 3

Related Questions