Search code examples
javamysqlsqlplayframeworkebean

Play & Ebean - get first record


How is it possible to get the first record of a table in my database using the play.db.ebean.Model.Finder, like a "first" method. The corresponding SQL would be:

SELECT * FROM my_table LIMIT 1;

Solution

  • Let's assume model Student

    @Entity
    public class Student{
    
    @Id
    public int id;
    
    public String name;
    }
    

    Model finder will be

    public static Finder<Long,Student> find = new Finder(Long.class, Student.class);
    

    Retrieve Student limit by 1

    public static Student getStudent()
    {
        // Use setMaxRows(limit_by)
        return find.setMaxRows(1).findUnique();
    }