Spring Data JPA - Users and Authorities relationship - spring

Is the relationship between Users and Authorities one to many or many to many?
Why choose one over the other when mapping out the Entities?
Is it just schemantics or will actually one type of relationship have a negative impact when retrieving/updating the Entities in some cases ?
I consider always a bidirectional relationship as it seems to be the one preferred in the framework.

Related

What would happen if entity model has relations but data (views) in database doesn't have relations and if they are inconsistent?

My question is would Spring JPA throw exceptions for every query?
I mean, let say there are tables without any relation (FK) between them in database. It is bad design but you cannot change it and it is not up to you.
But you know that data itself should be as there are relations.
That's why you create Entity model with all relations like they are there.
But as I said there is no real relations in database.
And in one point data are inconsistent in database.
Would Spring JPA throw exceptions if there are inconsistency or it will just return you inconsistent data?
I assume with "relations" you mean "foreign keys".
JPA doesn't care about foreign keys.
All it cares about is if the data matches the mapping information on the entities.
So if you have an entity A that references an entity B with id b but such a B does not exist you might eventually get an exception.
Or you might just get an A with a null reference to B.
If the reference is marked as mandatory you might actually not be able to load the A in the first place, because a join is used and therefore not returning any data at all.
Side note: All this depends more on the JPA implementation you are using than on Spring Data JPA.

Using 3NF in Laravel's ORM

I am trying to build a simple Laravel application. My data model looks like the following:
ENTITIES:
Project, Requirements, ProjectRequirementStatus
RELATIONSHIPS:
A Project has many Requirements
A requirement belongs to many Projects as a "ProjectRequirement"
A "ProjectRequirement" has one ProjectRequirementStatus
A ProjectRequirementStatus belongs to many Projects
TABLES:
projects
requirements
project_requirements
project_requirement_statuses
MODELS:
Project, Requirements, ProjectRequirementStatus
My question IS:
Is it improper to create a model for a relationship class? In this case, I would need to create a ProjectRequirement model and define the relationship to the ProjectRequirementStatus class.
I'm confused because most of my pivot tables include IDs of the two tables they are joining in a Many to Many, and typically, no additional relationships.
Am I thinking about this the wrong way? Are there "best practices" in terms of when a Model is created versus when it's not needed?
Using the 3NF in Laravel, you do not have to make models for the relationships. Laravel provides the Eloquent ORM which will provide the relationships without having to make the pivot tables models.
The Eloquent ORM also provides you a way to access data on pivot tables. (Defining The Inverse Of The Relationship)

Laravel: Table structure for multiple users types, polymorphic relationships

In my site (api using laravel 5.6 and laravel passport) I have two types of users (Teachers and Students), in the future there will be more. The teacher and student entities are very different, meaning that if I keep them all in one table, the table will be long and many fields will have a null value. Right now I have one Users table with common fields and two other tables (Teachers and Students) to which I have setup a polymorphic relationship from user. My question is if this is a good approach, or if there are other ways to handle this more elegantly?
I would create 1 table for Teachers and 1 table for Students and not use the Users table/model. This way you can keep them completely separate and not worry about adding more types of users in the future. Continually trying to fit new users into your existing Users model, which would be shared, is a headache. I made this same mistake when I started and eventually had to rework the project.
There are plenty of guides for Laravel multi-auth / multi-user online.
Here are a couple to help you get started:
https://medium.com/hello-laravel/multiple-authentication-system-laravel-5-4-ac94c759638a
https://www.codementor.io/okoroaforchukwuemeka/9-tips-to-set-up-multiple-authentication-in-laravel-ak3gtwjvt
Also, there are cases where it makes sense to use the User model for multiple types of users. For example, you may have multiple roles for a user where most/all of the fields are the same (not your scenario). In this case, you can assign a 'role' to each User and the check the roles for actions (e.g. add middleware to prevent roles from accessing various routes). Here is an example:
https://medium.com/#ezp127/laravel-5-4-native-user-authentication-role-authorization-3dbae4049c8a
Since you said the teacher and student entities are very different, you should keep them separate.

Querying multiple tables using jpa repository

Suppose if I have 3 entities - User, Skills, Department
and I have repositories corresponding to all of them - UserRepository, SkillRepository, DepartmentRepository.
I understand that the relation mapping between entities i.e. one-one many-many should be specified in the respective entity classes. The question is I want to use all of the 3 entities in a query. How would I do it? A single repository is associated with only one entity right? So, how/where would I write it?
As there are many different ways to specify queries with Spring Data JPA there are various answers to this.
Maybe you don't have to. If entity A references B and you just want to get the Bs with your A you simply use your ARepository to load As and use object navigation to get your Bs. You might read up on eager and lazy loading for more information about how to control this.
If you want referenced entities in the where condition you can use property paths in your query method names: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions
If you are using #Query annotations you can do (almost) whatever you want with JPQL. Among others, you may as well navigate properties to use them in where clauses.
In general, you'd put that query in the matching repository based on the primary entity returned.

Parse Relations: which Class should own it?

Using Parse.com "Relations", how do you determine which of the 2 classes should own the Relation?
For example think of WhatsApp groups.
Should User have a relation listing all the groups it subscribes to?
Or should Group have a relation listing all the users in the group?
And, does it make sense to have a relation in each? Duplicating the data?
Depends on if you want to store some metadata in one of the Classes. It is explained quite nicely in this part of the document:
https://parse.com/docs/relations_guide#manytomany-relations
The decision point here is whether you want to attach any metadata to
the relationship between two entities. If you don’t, Parse Relation or
using Arrays are going to be the easiest alternatives. In general,
using arrays will lead to higher performance and require fewer
queries. If either side of the many-to-many relationship could lead to
an array with more than 100 or so objects, then, for the same reason
Pointers were better for one-to-many relationships, Parse Relation or
Join Tables will be better alternatives.
In the whatsapp you have given, Since you have access to user, i think it should be more like what are the groups that user belongs to. Read the many to many relations assuming group as book and users as author. It will make sense

Resources