Sorry if there's any grammar / spelling mistakes in this post. I'll try to convey my questions as clear as possible.
Introduction
I have basic knowledge about Java Programming. I took Programming Courses at my Uni and somewhat familiar with Java and OOP concepts in general ( barely ).
After completing the course, I challenge my self to create a basic E-Commerce Application with Springboot as the Framework, in order to practice my self with the knowledge i got from the course.
This Application will be a Simple Multi Tenant Application, where each tenant can create a product page, and a customer can purchase. Mind you i create this app to challenge my understanding about Springboot, and will not be a complex app for use.
Set Up
In the tutorial, there's a usage of @ManyToMany to map Many to Many Relationship. I thought that i could use Many To Many Mapping on Tenants <-> Users Relationship. In my understanding, A tenantEntity could have multiple users assigned, and multiple usersEntity could belong to a tenantEntity
With that understanding, i try to create my tenantEntity and userEntity as follow
Now, how does one updates the data for the ManyToMany relationship?
Say, i want to modify the membership of a user. i want to add new tenant or remove existing tenant?
I try creating update method on my userRepository
// userRepository
public interface UserEntityRepository extends JpaRepository<UserEntity, Long> {
@Transactional
@Modifying @Query("""
update UserEntity u set u.username = :username, u.email = :email, u.firstName = :firstName, u.lastName = :lastName, u.isEnabled = :isEnabled, u.tenants = :tenants, u.updatedAt = :updatedAt where u.id = :id""")
int updateUsernameAndEmailAndFirstNameAndLastNameAndIsEnabledAndTenantsAndUpdatedAtById(
@Param("username") String username,
@Param("email") String email,
@Param("firstName") String firstName,
@Param("lastName") String lastName,
@Param("isEnabled") boolean isEnabled,
@Param("tenants") Set<TenantEntity> tenants,
@Param("updatedAt") Instant updatedAt,
@Param("id") Long id
);
}
resulting in a NullPointerExceptionjava.lang.NullPointerException: Cannot invoke "org.hibernate.sql.ast.tree.expression.Expression.getColumnReference()" because "pathSqlExpression" is null
in which resulted in a Hibernate Forum that states Updating *-to-many collections is not possible, but please submit a bug report to our issue tracker([https://hibernate.atlassian.net 14](https://hibernate.atlassian.net)) to improve the error message.
Questions
So what is the best practice of doing things with ManytoMany associations? I've found many tutorials that state the use of @ManyToMany Annotations, but left after they're done inserting things to the database. How can i update the join table relations when the need to modify the data arises?
Is there a knowledge gap between me and Springboot especially with JPA? should i take another approach?
I usually avoid ManyToMany relationships. Instead, I do them "manually" as I would directly in the database. If I want to relate A to B in a many to many relationship, I create C, which contains a ManyToOne (I don't remember if it's ManyToOne or OneToMany) relationship to A and a ManyToOne relationship to B. So basically C just contains a foreign key to A and another to B.