@ManyToMany delete not working with Spring Boot Refreshing Page Makes it wo

Hi All,

I am so new to Vaadin and Spring, it is a very frustrating stage but I am powering through. I am trying to integrate a many to many relationship in an H2 database with a vaadin front end. When I load the UI initially it allows me to add many to many relationships, but it does not remove the join when I remove them (observed using localhost:8080/h2-console).

The frustrating bit is if I refresh the web page everything works great. The project only has two entities to keep things as simple as possible.

The project is completely self contained, a wise experienced eye will be so much appreciated.

Regards
Johann
29526.zip (87.3 KB)

Hi,

This is not related to Vaadin, but I had a look at your code and I’d say your model is probably wrong. If what you really want is the user to manipulate book-publisher relationships, then you should create an Entity for that. Something like:

@Entity
public class Relationship {

@ManyToOne
private Book book;

@ManyToOne
private Publisher publisher;
....

}

But if what you really want is to offer a CRUD UI for Book, then you should delete the Book instance (not update it, as you are doing in your code). This however will remove the options in the other comboboxes of course.

Hi Alejandro,

Thank you for your response. The reason I thought it was vaadin related was that the add remove buttons work fine (most of the time) if I refresh the page after it is first initialised. So I wasn’t sure if there was something that happened on the second UI refresh instead of the initial load.

I am interested in managing both the relationships and the objects. Could you tell me why another Entity is needed, or reccomended intead of my implimentation?

I will see if I can impliment your suggestion thank you very much, for your help. I have watched many of your videos and they were very helpful.

It’s needed if you want the user to “CRUD” this kind of data, otherwise, how can you allow them to perform operations on book-publisher relations from Java code? Doing so, the code should be much simpler btw, as you wont’t need those methods to remove references from Collections, just remove the Relationship object and that’s it.

Hi Alejandro,

As per your reccomendation I have introduced another entity for the relationship. With the new implimentation as you mentioned I simply have to delete the relationship itself which works well. I have attached the new version, is this how you would have done it?

I noticed was in your code example above you did not specify primary key. Since only one relationship should exist between a particular book and publisher, is that something easy to impliment? As I think have a dedicated primary key would allow you to violate this constraint.

Again thank you for your help, is there any reading I can do on this design choice and proper implimentation that you know of?
29529.zip (92.2 KB)

Glad to know you got it working! I omitted @Id, getters, setters, imports to highlight my point. Regarding learning resources, I’d like
Hibernate’s documentation
and the
JPA chapters of The Java EE Tutorial
. I can also recommend Manning’s
Hibernate in Action
and Packt’s
Java Hibernate Cookbook
.