I have an sql query that I don't know how I can use it in play framework by fetching records from two different 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'
I have two mysql tables drivers
and Storebook
. I am able to get the list of data from StoreBook table but unable to access drivers table for join .
Look my code:
@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();
}
}
Drivers 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();
}
}
The following code is used in controllers :
public class showBooks_history extends Controller {
public static Result (){
Form<StoreBook> result = form(StoreBook.class);
return ok(showbooks.render(StoreBook.mystore(), result));
}
}
Code I use for view which is HTML Scala template:
@( 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>
}
....
you don't have relation in your StoreBook with Driver, firstly you should specify relation type (1-1, 1-m, m-m) ex:
//in StoreBook.java model
@OneToOne
public Driver driver;
then in template:
<tr>
<td><i>@i.driver.names</i></td>
<td><i>@i.bill_no</i></td>
</tr>