question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I flatten a nested reference?

See original GitHub issue

Lets say we have the normal Department entity, but we want the Employee entity to only show the department name, not the fully department object, e.g.:

interface Employee : Entity<Employee> {
    val id: Int?
    var name: String
   ...
    var departmentName: String
}

How would we define our bindings to accommodate this without storing the department name on the employee table?

object Employees : Table<Employee>("t_employee") {
    val id by int("id").primaryKey().bindTo { it.id }
    val name by varchar("name").bindTo { it.name }
   ...
    val departmentId by int("department_id").references(Departments) { /* somehow map this to deptName*/ }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vincentlauvlwjcommented, Feb 25, 2020

In this case, I’d prefer creating a DTO class and returning that instead, which is a general way as far as I know:

interface Employee : Entity<Employee> {
    val id: Int
    val name: String
    val department: Department

    fun toEmployeeDTO() = EmployeeDTO(
        id = id,
        name = name,
        departmentName = department.name
    )
}

data class EmployeeDTO(
    val id: Int,
    val name: String,
    val departmentName: String
)
0reactions
raderiocommented, Mar 15, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to flatten nested references into a simple target?
I have to map between a table specific POJO (flat structure) and a Domain object with nested objects.
Read more >
Flattening Nested Collections in Java | Baeldung
In order to flatten this nested collection into a list of strings, we can use forEach together with a Java 8 method reference:...
Read more >
Spark SQL - Flatten Nested Struct Column
In Spark SQL, flatten nested struct column (convert struct to columns) of a DataFrame is simple for one level of the hierarchy and...
Read more >
Flattening nested arrays - Amazon Athena
To flatten a nested array's elements into a single array of values, use the flatten function. This query returns a row for each...
Read more >
Convert nested JSON to a flattened DataFrame - Microsoft Learn
Use $"column.*" and explode methods to flatten the struct and array types before displaying the flattened DataFrame. Scala
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found