Ebean 如何从 play 框架中有关系的多个表中获取数据

Ebean how to fetch data from multiple tables which have relationship in play framework

我有一个 sql 查询,我不知道如何通过从两个不同的 tables 获取记录来在播放框架中使用它:

SELECT a.Id as DriverId, a.names as Drivernames, b.bill_no as driverBillNumber, b.DriverId from drivers a join StoreBook b on a.Id = b.DriverId where a.phone_number ='9889'

我有两个 mysql tables driversStorebook。我能够从 StoreBook table 获取数据列表,但无法访问 drivers table 以加入 .

看我的代码:

 @Entity
 @Table(name = "StoreBook")
public class Store extends Model {
@Id
public Long id;
@Constraints.Required
public String bill_no;
public int amount;
public String Created_at;


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


public static List<Store> mystore() {
    return find.all();      
    }
    }

司机 table :

@Entity
  @Table(name ="drivers")
   public class New_driver extends Model{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "Id", updatable = false, nullable = false)
   public Long Id;
   @Column
   @Constraints.Required
   public String names;
   New_driver(String names) {     
    this.names = names;
   }
    public static void create(New_driver account) {
    account.save();
    }
     }

控制器中使用了以下代码:

    public class showBooks_history extends Controller {
     public static Result (){
    Form<StoreBook> result = form(StoreBook.class);
    return ok(showbooks.render(StoreBook.mystore(), result));
      }
       }

我用于视图的代码是 HTML Scala 模板:

     @( formList: List[Store],form: Form[Store]) )
    @main_dashboard("welcome")
       {
          .....
          <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>Driver names</th>
                                <th>Bill number</th>
                            </tr>
                          </thead>
                          <tbody>
                          @for(i <- Store.mystore()) {
                                <tr>
                                    <td><i>names ???</i></td>
                                    <td><i>@i.bill_no</i></td>
                                </tr>
                                }
                               ....

您的 StoreBook 与 Driver 没有关系, 首先你应该指定关系类型 (1-1, 1-m, m-m) 例如:

//in StoreBook.java model
@OneToOne
public Driver driver;

然后在模板中:

 <tr>
    <td><i>@i.driver.names</i></td>
    <td><i>@i.bill_no</i></td>
 </tr>