Reputation: 551
I have a dictionary structure, that we need to pass to frontend. For now it provides different values for one dropdown, and available values for the rest of them dropdowns depending on the choice of first one
@Entity
@Table(name = "CAR")
@Data
public class Car{
@Id
@Setter(NONE)
Long id;
String name;
@ManyToMany
@JoinTable(name = "CAR_WHEELS",
joinColumns = @JoinColumn(name = "CAR_FK"),
inverseJoinColumns = @JoinColumn(name = "WHEEL_FK"))
Set<Wheel> wheels;
//some other similar sets
}
Now along the available values we want to have a default value for those, so we're thinking about something like:
@Entity
@Table(name = "CAR")
@Data
public class Car{
@Id
@Setter(NONE)
Long id;
String name;
AvailableValues availableValues;
DefaultValues defaultValues;
}
and then
@Embeddable
class AvailableValues{
Set<Wheels> wheels;
//...
}
@Embeddable
class DefaultValues{
Wheel wheel;
//...
}
But I don't know how to deal with the mapping side of it. The defaultValues should be plain and simple, either through adding those values to the CAR table, or through one-to-one relation instead of using Embeddable, but can't come up with any idea for the collections inside embeddable object, that would use already present structure.
Upvotes: 0
Views: 221
Reputation: 12122
I am not sure if I understood the question correctly, but DB schema would be preserved if a relationship mapping was just moved into AvailableValues
@Embeddable
@Data
class AvailableValues {
@ManyToMany
@JoinTable(name = "CAR_WHEELS",
joinColumns = @JoinColumn(name = "CAR_FK"),
inverseJoinColumns = @JoinColumn(name = "WHEEL_FK"))
Set<Wheel> wheels;
}
Optionally, if for some reason you wanted to change that mapping in an encompassing entity, you can use @AssociationOverride
@Embedded
@AssociationOverride(name = "wheels",
joinTable = @JoinTable(name = "MY_CAR_WHEELS",
joinColumns = @JoinColumn(name = "MY_CAR_FK"),
inverseJoinColumns = @JoinColumn(name = "MY_WHEEL_FK")
)
)
AvailableValues availableValues;
Upvotes: 1