kwojcikowski
kwojcikowski

Reputation: 173

Best practice for selecting "favourite" element from Set

I am facing the following problem: In my Spring Boot application there exists a person entity that can have multiple images. One of these images is marked as a default image - e.g. active profile picture. User can request person's general info (containing only default image) and person details (containing all of the images). As for now I have represented such scenario using the following:

Having person entity

class Person {

    var images: Set<Image> = mutableSetOf()

}

and Image entity

class Image {

    var isDefault: Boolean = false

}

I was wondering whether this is the best way to represent it. As an alternative this could be represented by defaulImage (representing the default one) and images (storing the rest) as two separate Person properites. This however will require more data manipulation on changing the default image (move previous default image to images, remove one object from images and set it as defaultImage). The application is running Neo4j database so instead of updating images properties it would require to modify the relations.

Which of the mentioned solutions is better? Is there any other, even better solution?

Upvotes: 1

Views: 332

Answers (1)

meistermeier
meistermeier

Reputation: 8262

It does depend highly on your use-case. Is the intention of your application to load all images nevertheless, go with the flag.

OR do you want only to load the defaultImage in 99% of the cases, create an additional relationship. In this case you could avoid loading all the unrelated images when you load a Person by using a projection like:

class PersonProjection {

    Image getDefaultImage();
    //... other information
}

https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#projections

Upvotes: 0

Related Questions