V
V
vitya_brodov2022-02-21 15:12:51
Java
vitya_brodov, 2022-02-21 15:12:51

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;
}

when creating a table from them, several tables are obtained:
6213818d2317e662371674.png
Question: How can I make the above described class make one table in the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2022-02-21
@vitya_brodov

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;

In my opinion, it's better not to bother like that. Imkho, it is more correct to create separate tables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question