0

I'm new to Ebean's world for play framework.

I have basically two classes, User and Book.

A user have several books. Also a user may have books with same title multiple time what ever the author or edition is. What i need is to link the books with user where same title of book will come only once whatever the other proprieties of the book is. That means retrieving the book list distinct on book's title.

Here it is what I've done so far

User Class

@Entity
public class User extends Model{
    @Id
    public int id;  
    public String name; 
    @ManyToMany(targetEntity = Book.class,cascade=CascadeType.ALL)
    public List<Book> book;
}

Book Class

@Entity
public class Book extends Model{
    @Id
    public int id;
    @Column(unique=true)
    public String name;
    public String author;
    public int edition;
    public int user_id;
}

But the multiple books having same title are not distinctly filtered. How to put the annotation correctly here so that I can only get one row for same title of book?

2
  • Looks like your question is more related to SQL than Ebean. Do you want to return all the book columns but distinct just by the title? Or do you need only the title column? Which database are you using? Commented Jan 20, 2016 at 3:11
  • I am using mysql. I need all the column distinct on title. Commented Jan 20, 2016 at 3:23

1 Answer 1

0

As I said, your problem is also related to SQL instead of just Ebean. First, here are some discussions about how to select multiple columns but distincting by a single one:

  1. mySQL select one column DISTINCT, with corresponding other columns
  2. How to select distinct value from one column only
  3. MySQL - SELECT all columns WHERE one column is DISTINCT
  4. SQL/mysql - Select distinct/UNIQUE but return all columns?

Then, you will need to execute the correct SQL query (based on the questions above) using Ebean:

public static List<Book> findDistinctBooks(String author) {
    String sql = "select b.id, b.name, b.author, b.edition from book b where author = :author group by name";
    RawSql rawSql = RawSqlBuilder.parse(sql)
            .columnMapping("b.id", "id")
            .columnMapping("b.name", "name")
            .columnMapping("b.author", "author")
            .columnMapping("b.edition", "edition")
            .create();

    return Ebean.createQuery(Book.class)
            .setRawSql(rawSql)
            .setParameter("author", author)
            .findList();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know using plain sql we can do it very easily. And also here book table is in 'one to many' relation with the user. So even your your query will not work here if you do not use join in the query. Anyway ebean orm we can obtain one to many relation very easily. My concern is whether there is any way to distinct the title in ebean orm with annotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.