AuditorAware is not working for Many-Many relationship - spring-boot

I use Spring data AuditorAware feature to audit CreatedBy,LastModifiedBy,CreatedDate and LastModifiedDate. It perfectly works for all the entities even when an entity is in one-many relationship. But It doesn't work for many-many relation ship.
For Ex,
I have entities user and user_group with many-many relationship. So it has another table user_group_association with user_id and user_group_id
Whenever there is an create or update in usergroup then audit columns get updated in usergroup table. But within the same transaction if any create/update to user_group_association there is no changes for these columns.
As I define many-many relation ship as below, I wouldn't have an entity class for user_group_association
// bi-directional many-to-many association to Users through
// user_group_association
#ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
#JoinTable(name = "user_group_association", joinColumns = #JoinColumn(name = "user_group_id"), inverseJoinColumns = #JoinColumn(name = "user_id"))
private Set<User> users;
How can I force Spring Data to make entires in user_group_association table as well ?

Related

What value to apply on one-to-one relationship with a reference data?

I need to persist an Entity named RevenueParty that has OneToMany relationship with another Entity RevenueAccount that further has OneToOne unidirectional relationship with another "reference" data Entity named RevenueCategory
At moment, I have defined the above relationships as follows:
In RevenueParty Entity
#OneToMany(mappedBy = "revenueParty", cascade = CascadeType.ALL, orphanRemoval = true)
private List<RevenueAccount> revenueAccounts= new ArrayList<>();
And in RevenueAccount, I have defined the following relationship:
#OneToOne
#JoinColumn(name="REVENUE_CATEGORY_ID")
private RevenueCategory revenueCategory;
My dilemma is as follows:
When I create a RevenueParty,the record in the corresponding table get created as expected, but nothing gets persisted in the table denoting the RevenueAccount Entity.
What can I do to make the records appear/persist for RevenueAccount Entity?
Thanks

Join query in Spring Data JPA

I have two tables:
Car_company which has the attributes of:
C_id (primary key),
C_name
Car_model which has the attributes of:
Com_id (referenced to C_id of Car_company),
Model_year
Warranty
I wish to access both of these tables individually and also I would like to perform a join operation on them and display all of the car_models along with their car_company name. I tried using both JPQL and native query but nothing worked. I also made sure to use the OneToMany and ManyToOne associations but I ended up getting infinite nesting,i.e, the models have car_company as field, this inturn has car_models as a list, and this keeps going. Please help me with entity classes and DAOs.
You can get a List of CarModel for each car company in the CarCompany entity through the oneToMany annotation like this:
#OneToMany(mappedBy = "carCompany", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<CarModel> carModels;
or get all car models with their company field in the CarModel entity like this:
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Com_id", referencedColumnName = "C_id", nullable = false)
private CarCompany carCompany;
Just try to define your relationships as LAZY for performance purpose and to prevent circular nesting when you map your responses to a Data Transfer Object (DTO).

Join table data gets deleted when updating an entity in ManyToMany Relationship

I have this Recipe entity with ManyToMany relationship with Category entity
#ManyToMany
#JoinTable(name = "recipe_category", joinColumns = #JoinColumn(name =
"recipe_id"),
inverseJoinColumns = #JoinColumn(name = "category_id"))
private Set<Category> categories;
The relationship in the Category entity is this.
#ManyToMany(mappedBy = "categories")
private Set<Recipe> recipes;
The Recipe, Category, Recipe_Category gets created along with a bunch of other tables based on other entities. However, when I edit Recipe, the data in the join table Recipe_Category gets deleted.
Why is this behavior and what I need to do to fix it?
Thanks in advance.

Retrieve data over a many-to-many relationship in a Spring/Hibernate project

I am new to development using Spring framework and Hibernate. And I need to know what is the best practice to retrieve data from multiple tables. Here is a simple scenario:
Assuming a many-to-many relationship with these tables: Students(sid, name), Courses(cid, title), Students-Courses(sid,cid)
I don't have a Model/Entity class for Students-Courses. Here is how I handled the relationship in the "Student" Model/Entity class:
#JsonIgnore
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(
name = "students-courses",
joinColumns = {#JoinColumn(name = "sid", referencedColumnName = "sid")},
inverseJoinColumns = {#JoinColumn(name = "cid", referencedColumnName = "cid")})
So, this class creates both "Students" and "Students-Courses" tables in the database via Hibernate. I also have a separate Model/Entity class for Course.
How can I get the list of all the students including their courses (titles)? I mean multiple records for the students with more than one course
I can create an attribute in the "Student" class like this:
private Set<Course> courses = new HashSet<>();
But it won't be returned back if I use a JpaRepository interface/class like
return studentRepository.findAll();
since it's not a #Column.
Should I write native-SQL code in my class?
Should I create an Immutable Entity class so Hibernate create sort of a database-view object in the data layer?
What is the best practice and how should I do that?

Insert data in intermediate table created by spring

I am using spring with JPA and I have 2 model classes
User
Resources
I have defined many to many relation using -
#ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinTable(name = "users_resources", joinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = #JoinColumn(name = "resource_id", referencedColumnName = "id"))
This definition has created another table users_resources.
To insert data in users_resources table, the REST api is sending just the User id [primary key] and Resource Id [primary key].
Is there anyway I can insert data into users_resources without fetching full user object and resource object? I wanted to use native query but it seems PersistenceJPAConfig has noway to run native INSERT query.
If you know Id of the object, and you need object reference (without object details) you could use EntityManager.getReference()
User userReference=em.getReference(User.class,userId);
Resource resourceReference=em.getReference(Resource.class,resourceId);
Using references will not exectue any SELECTs from the database.

Resources