Question: I am new to grails and implementing one-to-one relationship in grails and trying to query on tables and not sure on how to represent one-to-one relationship in domain classes and query the results.

I have two tables

```
car(car_id number primary key,name varchar2(255))
engine(eng_id number primary key,name varchar2(255),car_id number (foreign key to car_id))

```

**Domain Classes:**

```
class Car {
   ..
  static hasOne = [engine: Engine]
}

class Engine {
   Car car
   static belongsTo = [car : Car]
}

```

is the above one-to-ne relationship in domain class correct??

i need to query to get all the cars which have engines,should i use criteria query api or use HQL??

Any help appreciated.

Answer: 
Your issue is that the function has finished by the time the data arrives. This is due to the asynchronous nature of AJAX. You need to handle your data *when* it arrives, not assume it has arrived before it has.

```
function getNewValues(){
    $.ajax({
        dataType: 'json',
        url: 'get.php',
        type: 'POST',
        cache: false
    })
    .success(function(data) {
        // Handle your data here;
    })
    .fail(function() {
        console.log("An error ocurred");
    });

 // Attempting to use the data or returning the data here will not work!
}

```
Edit: Reread your question. Think [this response](https://stackoverflow.com/a/4293438/3503285) to a similar question could help.

Once you have executed the prepared statement, something like this could work instead of the PDO::FETCH\_ASSOC

```
...
$result = $statement->execute();
for ($file_data = array(); $row = $result->fetch_assoc(); $file_data[] = $row);

```

$file\_data is then an associative array of whatever data it is you want.

```
print json_encode($file_data);

```