Ebean persistence not working in Play 2.0 - java

To familirise myself with the Play 2.0 Framework, I've been following the Play 2.0 introduction tutorial. Unforunatly, I can't get the Ebean persistence to work. My Task.java looks like this:
package models;
import java.util.*;
import play.db.*;
import play.data.validation.Constraints.*;
import javax.persistence.*;
#Entity
public class Task {
#Id
public Long id;
#Required
public String label;
public static List<Task> all() {
return find.all();
}
public static void create(Task task) {
task.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
}
But on running the applicaton, find and save can't be resolved. I guess I forgot to import a module, but I can't figure out which on it is ...

I believe you need to extend Model class.
#Entity
public class Task extends Model {
....
}
Hope this helps.

As Dan W. already said, you need to extend the Model class:
#Entity
public class Task extends Model {
....
}
You also have to add the following static methode:
public static Finder<Long,Task> find = new Finder(
Long.class, Task.class
);
Also make sure you added following lines to your application.conf file to enable the database:
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"
ebean.default="models.*"

Related

Keep getting error "Unable to implement Repository method: RepoClass.updateAll(Iterable arg0). No possible implementations found."

Using Micronaut data v3, I created the following simple classes:
MyEntity.java
package test;
import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
#MappedEntity
public class MyEntity {
#id
#GeneratedValue(GeneratedValue.Type.AUTO)
Long id;
String name;
}
MyRepo.java:
package test;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;
#JdbcRepository(dialect = Dialect.MYSQL)
public interface MyRepo extends CrudRepository<MyEntity, Long> {
}
The project can build without above 2 classes. Once I add these 2 classes, I got the error
java: Unable to implement Repository method: MyRepo.updateAll(Iterable arg0). No possible implementations found.

Concern about importing class implementation into DAO

I have to implement DAOs which use only the interfaces of the objects. Now I'm having trouble figuring out how to use the em.find()of the EntityManagerclass.
My specific question is, if it is ok to import the implementation of a class directly into the DAO like in this example:
import dao.IStreamingServerDAO;
import model.IStreamingServer;
import model.impl.StreamingServer;
import javax.persistence.EntityManager;
public class StreamingServerDAO implements IStreamingServerDAO {
protected EntityManager em;
public StreamingServerDAO(EntityManager em) {
this.em = em;
}
#Override
public IStreamingServer findById(Long id) {
return em.find(StreamingServer.class, id);
}
}
I feel like I'm hurting some privacy principles by simply importing the model.impl.StreamingServer class into the DAO.
Problem is I don't know how else I'm supposed to get the needed class for the em.find()method.
Please not that I can't change the return type of the findByIdmethod as it's defined like this by the interface. (Also this implementation right now works as expected).

Eclipse BIRT create a Custom Emitter as class

I am trying to create a custom Emitter for BIRT. I created a Plugin, here works everything fine the problem is that I need it as a Java class.
The problem is when I try to Render in my own Emitter it says that the custom RenderOption is not supported.
Here is some of my Code:
IJsonRenderOption:
package org.eclipse.birt.report.engine.emitter.json;
import org.eclipse.birt.report.engine.api.IRenderOption;
public interface IJsonRenderOption extends IRenderOption {
public static final String OUTPUT_FORMAT_JSON = "json";
public static final String OUTPUT_EMITTERID_JSON = "org.eclipse.birt.report.engine.emitter.json";
}
JsonRenderOption:
package org.eclipse.birt.report.engine.emitter.json;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
public class JsonRenderOption extends RenderOption implements IJsonRenderOption {
public JsonRenderOption() {
super();
}
public JsonRenderOption(IRenderOption options) {
super(options);
}
}
I hope somebody can help me
Greetz
Try importing the following packages
importPackage(org.eclipse.birt.report.engine.emitter.json);
importPackage(org.eclipse.birt.report.engine.api.IRenderOption);
importPackage(org.eclipse.birt.report.engine.api.RenderOption);

Play Framework Does Not Create Models

I just downloaded the play framework from their site and am working through this tutorial.
I've noticed the framework creates the folders app/controllers and app/views, but not a models folder. I created it manually and added Task.java to it. When I get to the section entitled "Rendering the first page" and open localhost:9000/tasks I get a compilation error that says package play.models does not exist. Here is what my Task.java looks like:
package models;
import java.util.*;
public class Task {
public Long id;
#Required
public String label;
public static List<Task> all() {
return new ArrayList<Task>();
}
public static void create(Task task) {
}
public static void delete(Long id) {
}
}
Here is application.java, the file generating the compilation error:
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import play.data.*;
import play.models.*; // COMPILATION ERROR: "package play.models does not exist"!
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I believe it's supposed to be import models.Task; as opposed to import play.models.*;
That's quite confusing (IMHO) step in this tutorial, instead scroll down to Persist the tasks in a database section which describes preparing a model to cooperate with DB :) (it extends Model class, uses proper annotations, etc)
As you recognized it yet, you need to create a models package yourself.
Also as cYn wrote: you should import models like models.SomeModel into your controller
You are correct HukeLau_DABA , the Play will not create the models package for you. you have to create it.
I got these imports in my Application controller class. I got this sample play application running.
import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms._
import models.Task
and another thing in Eclipse is it will not import the necessary imports automatically.
it is bit pain now, once the IDE support get better I hope this will change.

how to import java extensions in a play framework model?

Hi I would like to import Play framework Java Exension* into a Play framework Model.
In specific I'd like to have in my model:
package models;
// various import
import play.templates.JavaExtensions;
#Entity
public class Product extends Model {
#PrePersist
public void save_slug(){
slug = title.slugify();
}
}
But I'm receiving following error
The method slugify() is undefined for the type String
What am I doing wrong?
*references:
- http://www.playframework.org/documentation/1.1/javaextensions#aslugifya
- http://www.playframework.org/documentation/api/1.2.4/play%2Ftemplates%2FJavaExtensions.html
Java extensions are static methods of JavaExtensions class, you can use them as follows:
slug = JavaExtensions.slugify(title);

Categories

Resources