skip to Main Content

I am a simple beginner that want to learn programming and I am a little confused

I have three entities: administrator (the only seller), customer and products. Beetween these entities has to be the next relation?

  • Administrator and Product (@OneToMany), Product and Administrator (@ManyToOne)
  • Customer and Product (@OneToMany), Product and Customer (@ManyToOne)

Administrator is the only seller and he selles its own products and the customer can buy more products. This is the main idea of my application.

Also, do I have to create authentication and authorization part before to start relations between entities?

Can someone tell me if is it a good approach? 🙂

I tried to understand better this concept of entities relation, but I am not sure how to implement them.

2

Answers


  1. It is not necessary to use bidirectional relationship between your entities. Just use it when you want to access each entity from the other entity in two ways.
    It is better to use something like this .

    @Entity
    public class administrator { 
    
    @Id
    @GeneratedValue(strategy= Identity)
    private Long id;
    
    @OneToMany
    @JoinColumn(name="administrator_id")
    private List<Customer> customers;
    
    // Other properties
    
    }
    
    
    
    @Entity
    public class Customer{
    
    @Id
    @GeneratedValue(strategy= Identity)
    private Long id;
    
    @OneToMany
    @JoinColumn(name="Customer_id")
    private List<Product> products;
    
    // Other properties
    }
    
    
    
    @Entity
    public class Product{
    
    @Id
    @GeneratedValue(strategy= Identity)
    private Long id;
    
    // Other properties
    }
    
    

    I think this kind of data model is better for your purpose.

    Login or Signup to reply.
  2. for customer and product you need to give the many to many relationship which means one customer can buy many products and one product can be buyed by many customer check the relation first .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search