#one-to-many double connection.

1 messages · Page 1 of 1 (latest)

graceful seal
#

I have entities Author and Product. Author has multiple products. So they have relationship One-to-many. I want to add connection One-to-many Category-Product. so Products will have 2 many-to-one relationships: with Author and Category. How do i do it?

@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "authors_generator")
@SequenceGenerator(name = "authors_generator", allocationSize = 1)
private Long id;
private String firstName;
private String lastName;
private String job;

@OneToMany(mappedBy = "author", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonManagedReference
private List<Product> products = new ArrayList<>();



@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "products_generator")
@SequenceGenerator(name = "products_generator", allocationSize = 1)
private Long id;
private Long idOfAuthor;
private String name;
private int height;
private int width;
private String color;
private String description;
private String image;

@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "authors_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private Author author;

foggy coveBOT
#

<@&1004656351647117403> please have a look, thanks.

foggy coveBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

t belongs to one Category and Category has multiple Products. This creates a double connection between Category and Product.

To implement this in a database schema, you can create three tables: Author, Product, and Category.

The Author table would have a primary key column (e.g., author_id) and other columns for author information.

The Product table would have a primary key column (e.g., product_id), a foreign key column referencing the Author table (e.g., author_id), and other columns for product information.

The Category table would have a primary key column (e.g., category_id) and other columns for category information.

To establish the one-to-many relationship between Author and Product, the foreign key column in the Product table references the primary key column in the Author table. This allows an author to have multiple products associated with them.

To add the one-to-many relationship between Category and Product, you would add a foreign key column in the Product table referencing the primary key column in the Category table. This allows a product to belong to only one category, while a category can have multiple products associated with it.

Here is an example of how these tables could be created:

Author:

  • author_id (primary key)
  • name
  • email

Product:

  • product_id (primary key)
  • author_id (foreign key referencing Author.author_id)
  • name
  • price

Category:

  • category_id (primary key)
  • name

With this schema, you can query for all products belonging to an author or all products belonging to a category.

graceful seal
#

hello, dude!

upbeat totem
#

your product class

@Entity
@Table(name = "products")
public class Product {

  // other fields...

  @NotNull
  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(name = "category_id", nullable = false)
  @OnDelete(action = OnDeleteAction.CASCADE)
  private Category category;
#

you can fetch category eager here

#

as it's just one object, but you're free to use LAZY

upbeat totem
#

i notice you don't use lombok

#

Category class would be like

@Entity
@Table(name = "categories")
public class Category {
    
    // other fields...

    @OneToMany(
        mappedBy = "category", 
        cascade = CascadeType.ALL, 
        orphanRemoval = true, 
        fetch = FetchType.LAZY
    )
    private Set<Product> products = new HashSet<>();

    // getters, setters
}
graceful seal
#

but i use lombok for DTO

upbeat totem
#

and you define your constructor

#

you can fetch category eager here
i think it's better to go with LAZY

graceful seal
upbeat totem
#

the DTO classes are for that

#

that annotation is useful when you want to serialize/deserialize objects having bidirectional relationships

upbeat totem
#

along with @JsonManagedReference

#

@JsonBackReference must be on the list/set attribute

graceful seal
#

orphanRemoval = true - never used it, but read about it. Can you explain this thing?

upbeat totem
#

in that case, it gets deleted automatically

#

that case is called orphan entity

graceful seal
upbeat totem
#

but has no rights yet

#

in your properties file use

#
spring.jpa.hibernate.ddl-auto: update
graceful seal
#

was already there

upbeat totem
#

Ah you have some products without category

#

You need to clear the table

graceful seal
#

oh ok

upbeat totem
#

Or make category nullable

#

In code

graceful seal
upbeat totem
#

Change nullable to true

#

And change NotNull to Nullable

graceful seal
#

oh

#

then i will prefer to drop

upbeat totem