Hello, I ran into the problem of not knowing Hibernate. In general, I have two tables with dependent columns. For example, there is a car service table:

id | title | picture

The second table stores service data, for example:

id | tire fitting | wheel replacement | 200 | service id

Questions:

  • How can I make dependencies between columns using Hibernate annotations?

  • How can I get a service object in which the list of service objects for this service will be stored?

For example, how to make the architecture such that when accessing repository.findAll() I can get an AutoService object with a List<> service field?

 @Entity @Table(name = "AutoRate") public class AutoService { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") private long id; @Column(name = "serviceName", nullable = false) private String serviceName; @Column(name = "imageURL", nullable = false) private String imageURL; @Column(name = "mapCoordinate", nullable = false) private String mapCoordinate; @Column(name = "websiteURL", nullable = false) private String websiteURL; @Column(name = "phoneNumber", nullable = false) private String phoneNumber; public AutoService() { } public long getId() { return id; } public String getServiceName() { return serviceName; } public String getImageURL() { return imageURL; } public String getMapCoordinate() { return mapCoordinate; } public String getWebsiteURL() { return websiteURL; } public String getPhoneNumber() { return phoneNumber; } } 

And the service class for the service:

 @Entity @Table(name = "Service") public class Service { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") private long id; @Column(name = "serviceName", nullable = false) private String serviceName; @Column(name = "category", nullable = false) private String category; @Column(name = "price", nullable = false) private int price; @Column(name = "autoServiceId", nullable = false) private long serviceId; public Service() { } public long getId() { return id; } public String getCategory() { return category; } public int getPrice() { return price; } public String getServiceName() { return serviceName; } } 
  • This question refers to Hibernate, not Spring - Victor Khovanskiy

1 answer 1

Add the following field to the AutoService class:

 @ManyToMany @JoinColumn(name = "serviceId") List<Service> services;