Hibernate JPA scenario to solve? - spring

There is a class Consultant. A consultant can have many kind of experience like salaried, self-employed, freelancer. For each type of experience there are different data to save in database.
Salaried:
Total Experience
Company Name
Experience Time in years
Offer/Reveling letter Link
Self Employed:
Company Name
Total Experience
CIN_Number
GST_Number
CompanyCertificateLinkUrl
FreeLancer:
Total Experience
A user can have experience in more than one occupation type like a consultant is both salaried and freelancer, or self employed plus salaried and freelancer. So i am confused how to make the #Entity class for this type of use case.
My Solution
#Entity
class Consultant{
#Id
int id;
#OneToOne
Salaried semp;
#OneToOne
SelfEmployed selfemp;
#OneToOne
Freelancer femp;
}
But i think this is not good practice as it will lead to many null field in the database.
ANY BETTER SOLUTION

I think your approach is fine. #OneToOne fields are optional by default, so can be null. That means there wouldn't be a row in the corresponding tables, so you'd only have up to two null values per row in the Consultant table.
If you're really concerned about nulls in the database, then you can map the relationships the other way, so:
#Entity
class Consultant{
#Id
int id;
#OneToOne(mappedBy = "consultant")
Salaried semp;
#OneToOne(mappedBy = "consultant")
SelfEmployed selfemp;
#OneToOne(mappedBy = "consultant")
Freelancer femp;
}
This way, if there's no row in the Salaried table that's related to the Consultant, the semp field will be null in the Consultant object.

you can do with two classes consultant and profession(id,name) and the relation OneToMany,ManyToOne
Consultant's entity
#Entity
class Consultant{
#Id
private int id;
#OneToMany(mappedBy = "consultant",cascade = CascadeType.All)
List<ConsultantProfession> cp;
}
Profession's entity
#Entity
class Profession{
#Id
private int id;
private String name;
#OneToMany(mappedBy = "profession", cascade = CascadeType.All)
private List<ConsultantProfession> cp;
}
ConsultantProfession's entity
#Entity
#Table(name="consultant_profession")
public class ConsultantProfession{
#Id
private int id;
// this is the link to the consultant class
#ManyToOne
#JoinColumn(name="consultant_id")
private Consultant consultant; // this name is specified in the class seeing patients as value of parameter `mappedBy`
// this is the link to the profession class
#ManyToOne
#JoinColumn(name="profession_id")
private Profession profession;
}

Related

Two tables connected via Primary Key

I have read about the use of #MapsId and #PrimaryKeyJoinColumn annotations, which sounds like a great options. I have two tables (UserList and UserInformation) which have a child, parent relationship, respectively; both classes below are abbreviated to just include the relevant columns. UserInformation's primary key value is always null and does not take the value of its parent column.
User Class
#Entity
#Data
#Table(name = "user_list")
public class UserList {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
// List of foreign keys connecting different entities
#OneToOne(mappedBy = "user")
#MapsId("id")
private UserInformation userInfo;
}
UserInformation Class
#Entity
#Data
#Table(name = "user_information")
public class UserInformation implements Serializable {
#Id
private Integer userId;
#OneToOne
private UserList user;
}
I would prefer to not use an intermediary class if possible. I'm not tied to MapsId or even this implementation if there is a better solution.
Thanks!
The question is not very clear to me, but I think you could improve the following in the modeling of the entity:
The #column annotation can only be omitted when the class parameter is called exactly the same as the database column, taking into account the table name nomenclature, could it be that the column is user_id ?, if so the id parameter should be :
#Id
#column(name="USER_ID")
private Integer userId;
In the user entity being id, it will match the DB ID field so the #column annotation is not necessary

How to make sure country is not added twice in mysql database in hibernate using spring mvc one to many relationship?

I have two entity first one is Student and the other one is an address. It is one too many relations ie one address can have many students. Now I have is a registration page. When I first register a student say with country name united states, it is saved in database giving primary id as 1 to united states and correspondingly gives correct id in student's database. But when I again try to register the next student with different information but the same country in my case united states it gives me a new primary key for the same country. But as this one is, one to many relationships I am thinking if there is anything in hibernate that maps to the same id in address database, therefore, I will only have one value of the united states in the address database. I need to have only a single entry of the united states a database. What is the appropraite way of doing need? Thank you
This one is Address table
#Entity
#Table(name = "tbl_address")
public class Address {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "address_id")
private int addressId;
private String country;
#OneToMany(targetEntity = Student.class, mappedBy = "address")
private List<Student> student;
This one is Student table
#Entity
#Table(name = "tbl_student")
public class Student {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "student_id")
private int studentId;
#Column(name = "first_Name")
private String firstName;
#Column(name = "second_Name")
private String secondName;
private String email;
#Column(name = "mobile_no")
private float mobileNo;
#DateTimeFormat(pattern = "yyyy-MM-dd")
#Temporal(TemporalType.DATE)
private Date dob;
private String gender;
#ManyToOne(cascade = {CascadeType.MERGE , CascadeType.ALL} )
#JoinColumn(name = "address_id")
private Address address;
}
This one is just the implementation in StudentRepositoryImpl class
#Override
public void saveUserInfo(Student user) {
Session session = HibernateUtil.getSession(sessionFactory);
session.save(user);
}
#Override
public void saveAddressInfo(Address address) {
// TODO Auto-generated method stub
Session session = HibernateUtil.getSession(sessionFactory);
session.save(address);
}
First option. On UI you should have a list of available countries (so I expect you already have populated country table in database). So UI will display to users available country names, but on backend side you will operate with countryId.
Second option. In case on UI you want users insert any string as country name, and if you want to register country on any new provided country name. For that you need to have unique index in country table on country name (it's up to you to decide whether it will be case insensitive or not).
Before saving student entity, you should fetch available country by it's name. if such one exist - use countryId with saving studend. if not exist - create a new country entity, and use generated countryId in studend entity.
Second option is risky in that way users could provide any values for country names, and probably with typo, so the first option is preferable.
Have a separate table for Country (That would be called master data). Then the Address entity would be something like this:
#Entity
#Table(name = "tbl_address")
public class Address {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "address_id")
private int addressId;
#ManyToOne
private Country country;
#OneToMany(targetEntity = Student.class, mappedBy = "address")
private List<Student> student;
You can get the list of students doing a join query with Country and Address tables.
Hope this is helpful

spring data - how to make unique constraint with custom logic?

using spring data, I created User 1:N UserDog N:1 Dog relation. Both 1:N relations are unidirectional #ManyToOne with UserDog being the custom relation table.
User entity:
#Entity
public class User {
#Id
#GeneratedValue
private long id;
#Column(nullable = false)
private String name;
}
Dog entity:
#Entity
public class Dog {
#Id
#GeneratedValue
private long id;
#Column(nullable = false)
private String name;
}
User dog relation table:
#Entity
public class UserDog {
#Id
#GeneratedValue
private long id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn
#OnDelete(action = OnDeleteAction.CASCADE)
private User user;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn
#OnDelete(action = OnDeleteAction.CASCADE)
private Dog dog;
#Column(nullable = false)
private Instant createdOn = Instant.now();
#Column
private Instant disabledOn;
}
Use case
Use case is to store history of User-Dog bindings, where the concrete Dog can be bound only to one User at the time. That's why I added createdOn and disabledOn columns to UserDog. disabledOn being null indicates that the relation is active and the Dog can't be assigned another User. If disabledOn is not null, then the record is stored only for evidence purposes and the Dog can be assigned to the same or another User again.
Question
How to ensure that the combination of Dog's id and disabledOn being null is unique in UserDog table?
In pseudo code I want something like this:
#Entity
#UniqueConstraint({#UniqueConstraint(this.dog.id), #NullConstraint(this.disabledOn)})
public class UserDog {...}
You can simply create a unique constraint for dogId and disabledOn.
It does add the limitation that no two relationships may end at the same time but this seems to fit your use case.

Design decision while using one to one relationship

I have following entities, I am in doubt about the design design like should DRIVING_LICENSE table contain foreign key PERSON_ID or PERSON table should have LICENSE_NUMBER as foreign key from DRIVING_LICENSE table?
If PERSON table has LICENSE_NUMBER then PERSON table will be child table and DRIVING_LICENSE will be parent table, so it implies that when a driving license is deleted then the person should be deleted.
On the other way if DRIVING_LICENSE will have a PERSON_ID then in uni directional one to one relationship in hibernate we will not be able to have a reference of DrivingLicense instead we will have a reference of Person in DrivingLicense, but most of the time it requires that we access Person not DrivingLicense.
Above two are my main doubts? What is the correct choice and what are its pros and cons?
DrivingLicense.java
#Entity
#Table(name = "DRIVING_LICENSE")
public class DrivingLicense {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "LICENSE_NUMBER")
private int licenseNumber;
#Column(name = "DATE_OF_ISSUE")
private Date dateOfIssue;
#OneToOne
#JoinColumn(name = "PERSON_ID")
private Person person;
}
and
Person.java
#Entity
#Table(name = "PERSON")
public class Person {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "PERSON_ID")
private int personId;
#Column(name = "PERSON_NAME", nullable = false, length = 30)
private String personName;
#OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
private DrivingLicense drivingLicense;
}
Drivers License should contain a NOT NULL foreign key to person with a unique constraint and here's why:
Every License must be associated with a person.
A person may have zero or one license associated with them.
Since the license must be associated with a person but a person does not need to have a license, the foreign key should be held by the license table.
The unique constraint on the foreign key will enforce the one-to-one relationship. Without it you'd have a one-to-many relationship.

How to save an object in two different tables using Spring MVC and Hibernate

Let's consider the following:
#Entity
#Table(name="person")
public class Person implements Serializable {
#Id
#GeneratedValue
#Column(name="id")
private int id;
#Column(name="firstName")
private String firstName;
#Column(name="lastName")
private String lastName;
...getters/setters...
}
and
#Entity
#Table(name="car")
public class Car implements Serializable {
#Id
#GeneratedValue
#Column(name="id")
private int id;
#Column(name="brand")
private String brand;
#Column(name="model")
private String model;
#Column(name="personId")
private String personId;
...getters/setters...
}
Let's imagine that a user is going to subscribe and enter his personal info, like first name, last name, the brand of his car, as well as the model of the car.
I do not want the personal info of the person to be stored in the same table than the car info.
I also would like to be able to retrieve the car information with the personId, this is why I have personId in the Car class.
Which annotations should I use to be able to accomplish this? Obviously I will need a constraint on the Car table and make personId a foreign key, right? What is the best way?
I have seen different things, what is the best?
In Car class, replace
#Column(name="personId")
private String personId;
with
#ManytoOne(fetch = FetchType.LAZY)
#CJoinColumn(name="person")
private Person person;
In Person class, add
#OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
private List<Car> cars;
You are now forming bi-directional one-to-many which means you can retrieve cars of person and person (ownder) of the car.
The cascade allows saving or updating of cars when person is saved. All cars are also deleted when person is removed.
It depends on your requirements.
If you want to use the same vehicle for multiple users, then you shall make it an entity, and use a many-to-many relationship.
If you don't want to change your entity structure at all, but just the database mapping then look at #SecondaryTable and #SecondaryTables annotations, they define more tables for an entity, and then you shall specify which table to use for each column (otherwise they are assigned to main table).

Resources