Answer the question
In order to leave comments, you need to log in
How to make one table from nested objects in a class?
I have a class which has other nested objects.
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class BaseEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name="UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
@JsonIgnore
private UUID id;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private Curator curator;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private FinAccounts finAccounts;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private Activity activity;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private Address address;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private Partner partner;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private RateType rateType;
private String retentionSchem;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "id")
private Tarif tarif;
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
The answer is no.
As you yourself said, these are objects. So, their values will be stored in other tables. And this object with the help of foreign key will refer to the nested object.
There is, however, the following option (if it suits you):
If the object can be embedded, then you can use the annotations @Embeddable & @Embedded
https://www.baeldung.com/jpa-embedded-embeddable
instead of the entity. in fact, the columns of the embeddable object will be added to the main entity
PS there is another option for implementing your idea. Perhaps not the best.
In general, if you are using a postgres database, then the object can be stored in jsonb format.
There is no solution out of the box, but you can connect additional. lib.
Here is a similar question:
https://stackoverflow.com/questions/51276703/how-t...
Dependency:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.2.2</version>
</dependency>
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private List<Child> children;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question