0

I have two tables orders and address

Order table
+----+----------------+-----------------------------------+--------------------------------------------------+
| id | trackingNumber | user_id (reference to user table) | receiverAddress_id (reference to address table ) |
+----+----------------+-----------------------------------+--------------------------------------------------+
| 1  | TRACK123       | 1                                 | 2                                                |
+----+----------------+-----------------------------------+--------------------------------------------------+
|    |                |                                   |                                                  |
+----+----------------+-----------------------------------+--------------------------------------------------+
address table
+----+--------+--------------+--------------+
| id | name   | addressLine1 | addressLine2 |
+----+--------+--------------+--------------+
| 1  | Mr.abc | qwertyuiop   | qwertyuiop   |
+----+--------+--------------+--------------+
|    |        |              |              |
+----+--------+--------------+--------------+

Need help in activeJdbc joining these two tables and get address as the children in JSON. I have tried the relation annotatins however unable to get the desired result.

//sample code: not working
  LazyList<Orders> order = Orders.find("id = 1").include(Addresses.class);
  order.toJson(true);

Hexgear
  • 320
  • 1
  • 3
  • 13
  • First, you need to include actual table names as well as model definitions. In addition, you are using a model `Pickups` so, it is not clear at all what your table relationships are. Please, include more detail. – ipolevoy Sep 21 '20 at 15:15
  • Hi ipolevoy thanks for reply, In the sample code I have shared the wrong my mistake. my Address model looks like: `@Table("addresses") public class Addresses extends Model { ` Order Mode is: `@Table("orders") @Many2Many(other = Addresses.class, join = "orders", sourceFKName = "receiverAddress_id", targetFKName = "id") public class Order extends Model { ` however when json is gerenated `"children":{ "addresses":[ ]` is empty. I am unable to figure out to realed these two tables – Hexgear Sep 22 '20 at 17:14
  • if I use `@Table("addresses") @HasMany(child = Orders.class, foreignKeyName = "receiverAddress_id") public class Addresses extends Model {` ` I get the address node in json `"parents":{ "addresses":` . Howver the key is `parents` is their any way to overide it. – Hexgear Sep 22 '20 at 17:26
  • hi changed `@Table("orders") @Many2Many(other = Addresses.class, join = "orders", sourceFKName = "id", targetFKName = "receiverAddress_id") public class Order extends Model {` it worked – Hexgear Sep 22 '20 at 17:43
  • glad it worked for you. However, best if you edit your actual question and provide all configuration I was asking about so that I can provide a better answer and close this question – ipolevoy Sep 23 '20 at 20:01

0 Answers0