Ebean如何从多个表中获取数据,这些表在play框架中具有关系

时间:2017-05-12 00:41:45

标签: jpa playframework hibernate-mapping playframework-2.2 ebean

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

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表driversStorebook。我可以从StoreBook表中获取数据列表,但无法访问 drivers 表进行连接。

查看我的代码:

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

驱动程序表

@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>
                                }
                               ....

1 个答案:

答案 0 :(得分:0)

你的StoreBook with 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>