There are entiti in the project, between which are configured ManyToMany links. For example, Menu and Place. In the database, this data is stored in the place_has_menu table:

enter image description here

And everything is good, but only these tables are not used at all. They are simply listed in the database, but when you need to pull out all the menus, for some kind of place, they use joins, but they do not refer to this table. Is it worth it in this connection to change the connections between entiti like ManyToOne, OneToMany and remove these tables from the database? And even if you change the connections, how will the architecture change from this and will it be necessary for the ManyToOne and OneToMany connections of the table, as in the screenshot or will it reduce a little the base weight?

Update1 : Menu.class:

@Entity @Table(name="menu") public class Menu { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column private Long id; @JoinColumn(name="name") private String name; @ManyToMany(mappedBy="menus", cascade = CascadeType.ALL, fetch=FetchType.EAGER) private Set<Place> places = new HashSet<Place>(); // getter setters } 

Update2 There was still such a question. I have a method, for example:

 public void addByForm(PlaceForm placeForm, MultipartFile multipartFile){ Place place = new Place(); PlaceType placeType = placeTypeDao.getById(placeForm.getTypeId()); place.setName(placeForm.getName()); place.setFile(fileService.saveFile(multipartFile, "place")); place.getPlaceTypes().add(placeType); placeType.getPlaces().add(place); } 

If you change the connection between the entities, will it affect the work of such methods? Or adding a delete slash will work on the same principle?

  • those. Do you have a link to Place in Menu? - zRrr
  • @zRrr updated the question. And another question? If you change the connection, will it somehow affect the services of adding and deleting menus from any one Place? - raviga

1 answer 1

I did not quite understand the question, but I will try to answer all the same. First of all, you need to understand the business specifics for which this database is designed. In one case, for the same entities, it is quite possible that the Один ко Многим relationship will suffice, in the other, only Многие ко Многим .

Regarding entities, they should always reflect the real state of affairs, and if for some reason there is a field in the database, a table or something else that is not already used, it’s best to remove it so as not to add confusion and mess to the project.

Therefore, before deleting the table, ask yourself the question, and what relation in my case do I need between the Menu and Place entities. After that, the answer to the question " delete or leave or change something else " will be obvious.

  • I updated the question, as another nuance arose - raviga
  • one
    Again, depending on what and how you change. Perhaps, changing the relationship between the entities will be all good, and maybe it will fall somewhere. In any case, if any problems arise, no one bothers you to correct the "problem code" to reflect the current realities. - sp7