How can I create Join table in MySQL database using hibernate in Java? - mysql

I have two entity table called appointment and service. One appointment can be many services. Services already store in the database. We can newly add appointment. When add new appointment we selected added services from the drop down menu. Can add many services with one appointment. I need to store new record on appointment table and store relevant appointment id and services id's on another join table. Here attached image with my problem.
Already I tried many ways for do this. Bellow is one attempt.
Here is appointment class
#Entity
#Table(name="appointment")
public class Appointment extends AbstractPersistable<Long> implements Serializable {
#OneToMany
#JoinTable(name = "service_x_appointment", joinColumns = #JoinColumn(name = "appointment_id"),
inverseJoinColumns = #JoinColumn(name = "beautyservice_id"))
Set<BeautyService> beautyServices;
private String type;
private Date date;
private String time;
private String description;
private int approval;
#ManyToOne
#JoinColumn(name = "userid")
private User user;
//getter and setter
}
Here is BeautyService class
#Entity
#Table(name="beauty_service")
public class BeautyService extends AbstractPersistable<Long> {
private String serviceName;
private String timeDuration;
private String amount;
//getter and setter
}
Here is appointment controller class code,
#RequestMapping(value="/createAppointment",method = RequestMethod.POST)
public String createAppointment(#RequestBody Appointment appointment){
String response = null;
response = appointmentService.save(appointment);
return response;
}
Here is appointment service class code
public String save(Appointment appointment) {
appoinmentRepository.save(appointment);
return "Appointment added successfully";
}
Here is the my request body.
{
"type":"Type02",
"date":null,
"time":"20:56",
"description":"Hellow World",
"approval":0,
"user":{
"id":2,
"name" : "Alex",
"telephone" : "0774466886",
"age":21,
"email": null
},
"beautyServices" : [
{
"id":1,
"serviceName":"hair strate",
"timeDuration" : "02 Hours",
"amount" : 5000
},
{
"id":2,
"serviceName":"Eye brows",
"timeDuration" : "02 Hours",
"amount" : 5000
},
{
"id":3,
"serviceName":"Near cutting",
"timeDuration" : "02 Hours",
"amount" : 5000
}
]
}
Why not record in the join table? Only appointment table.

You can do it in multiple ways. One has been specified #Aritra Paul, which is actually Bidirectional representation of OneToMany mapping.
I think you want to use UniDirectional representation. In that case you won't have to use mappedBy.
Just create your entities like below:
#Entity
#Table(name="appointment", schema="ADMIN")
public class Appointment implements Serializable {
#OneToMany
#JoinColumn(name = "appointment_id")
#JoinTable(name = "service_appointment", joinColumns = #JoinColumn(name = "appointment_id"),
inverseJoinColumns = #JoinColumn(name = "service_id"))
Set<Service> services;
}
#Entity
public class Service {
// Some Properties. No need to specify reference of Appointment here.
}
If you define your entities like that you will have a join table like this
+----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| appointment_id | bigint(20) | NO | MUL | NULL | |
| service_id | bigint(20) | NO | PRI | NULL | |
+----------------+------------+------+-----+---------+-------+
Hope this helps!!

You definitely shouldn't create Joined table entity as you mentioned as it's more the underlying database representation than the object oriented one.
You can achieve the join table by defining something like:
#Entity
#Table(name="appointment", schema="ADMIN")
public class Appointment implements Serializable {
//...
#OneToMany(mappedBy="appointment")
#JoinTable(name="Join_Table")
Set <ServiceT> service;
use relation mapping ManyToOne or OneToMany according to your table.
#Entity
#Table(name="service", schema="ADMIN")
public class ServiceT implements Serializable {
//...
#ManyToOne
Appointment appointment;
If you want to explicitely set your column name you can use
#JoinColumn
annotation.

Can you please check if the beautyServices are actually being bound from the #RequestBody properly.
The property name is beautyServices while in Json it says "service".
In the Json it should say "beautyServices" instead of "service".
Also, check if the ids 1, 2, 3 already pre-exists on the beautyServices table.
If Not, if you want those to get inserted as well by saving appointment, you need to add CASCADETYPE.PERSIST.

Related

Post JSON with #ManyToOne Relationship in Spring Boot

I have written a test application in spring boot. Employees have a relation to a department. CRUD works, but I'm not sure I'm doing it the correct way.
When I will create a new employee I have to send the following post request
"id": 3,
"firstname": "John",
"lastname": "Doe",
"salary": 50000,
"department": {
"id": 2,
"name": "Sales"
}
}
This is the employee class:
#Entity
public class Employee {
#Id
#GeneratedValue
private Long id;
private String firstname;
private String lastname;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "department_id", referencedColumnName="id")
private Department department;
private int salary;
This is the create method in the EmployeeController:
#PostMapping("/employees")
public Employee create(#RequestBody Employee employee) {
return employeeService.add(employee);
}
The department entries already exists.
Is it possible to create an employee without filling the complete relation
(department)?
I would like to add the department id only. But if I do this, the name field in the json data is empty (get request)
id 3
firstname "John"
lastname "Doe"
department
id 2
name null
salary 50000
Is there any better approach?
You are taking an Entity as a request body which is not the correct approach. You can take Some VO as Request Object and then convert to Entity inside a service.
For E.g
EmployeeVO{
private String firstname;
private String lastname;
List<Integer> departments;
}
Also, you can use Jackson annotations like
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL) to ignore unknown attributes and include not null attributes.

JPA serialize enttiy

I have my data model that contains 3 tables: User, Profile, UserProfile.
public class User implements Serializable {
private Integer id;
......
#OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch =
FetchType.LAZY)
#JsonManagedReference
#JsonProperty("profiles")
private List<UserProfile> userProfiles = new ArrayList<UserProfile>();
}
public class Profile implements Serializable {
private Integer id;
......
#OneToMany(mappedBy="profile", cascade = CascadeType.ALL, fetch =
FetchType.LAZY)
#JsonBackReference
private List<UserProfile> userProfiles= new ArrayList<UserProfile>();
}
public class UserProfile implements Serializable {
private Integer id;
#ManyToOne
#JoinColumn(name = "idUser")
#JsonBackReference
private User user;
#ManyToOne
#JoinColumn(name = "idProfile")
#JsonManagedReference
private Profile profile;
}
And here’s my json feed back:
{
"id": 1,
.......
"profiles": [
{
"profile": {
"id": 1,
.....
},
{
"id": 2,
.....
}
}
]
}
I have two questions:
Is it possible to remove the profile attribute and have:
{
"id": 1,
.......
"profiles": [
{
"id": 1,
.....
},
{
": 2,
.....
}
]
}
In a manytomany relationship with an intermediate table that contains a primary key (id), 2 foreign key that are the ids of the 2 tables that have the manytomany relationship, is that how to do it?
For the 1st question, to hide profile attribute, there are 2 options:
1. If you don't need it in any json output, you can add a #JsonIgnore annotation to it;
2. If you need it elsewhere but don't want it here, you can use Projection. Check https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections and https://www.baeldung.com/spring-data-rest-projections-excerpts for reference on how to use projections.
Checked your code again. Your code has some problem.
You only need 2 entities: User and Profile. And just add #ManyToMany relationship to them.
Refer here for a complete sample on ManyToMany https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

Spring Data, MySQL and not working unique value

I'm having troubles getting into Spring Data
I got entity Product which has Category (I'm guessing relation type is right? Product has one Category, Category has many products)
#Entity
public class Product implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = "category_id")
private Category category;
}
#Entity
class Category implements Serializable {
public Category() {
}
public Category(String name){
this.name = name;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "category_id")
private Long id;
#Column(unique = true)
private String name;
#OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Product> products;
}
Now I try to add new Product via Postman, calling my RestController
#PostMapping("/add")
public Product addProduct(#Valid #RequestBody Product product){
return repository.save(product);
}
With 2 following requests
{
"name" : "pork",
"category" : "meat"
}
{
"name" : "chicken",
"category" : "meat"
}
In the result I got 2 following responses
{
"id": 1,
"name": "pork",
"category": {
"id": 1,
"name": "meat",
"products": null
}
}
{
"id": 2,
"name": "chicken",
"category": {
"id": 2,
"name": "meat",
"products": null
}
}
And on database I actually got 2 categories named "meat" (even tho it should be unique. What's more, do I actually need Set<Product> in my Category class? TBH, Category has no intrest in that at all.
There are a few problems with your code.
You are directly using entity as the rest API model. Suggest to create a separate ProductModel with only fields that client has access to.
You mixing category creation together inside product creation, but your category in the request only contains name. To the backend, unless you check whether such a category exists, it's always treated as a new category.
Before you call repository.save, you need let category knows what's the product inside. In your current code, only product know its category.
You don't need Set products in your Category class (and it's recommended to use only #ManyToOne).

How to access spring jpa references in json

I have two entitys, A and B. Lets say, that, A has some fields (name, location). B has some fields too + a #ManyToOne relationship to A.
Now if I run my app, I can see the entitys and their valus in ...myDomain/api and specific entitys in ...myDomain/api/A for example. Now if look at ...myDomain/api/B/1, I can see B-s values + under _links a reference to A. How can I get A to already be as a value in B, not a link.
End result should look smth like this:
{
"_embedded" : {
"B" : [ {
"id" : 1,
"someData" : "data",
"otherData" : "other",
"A" : {
"name": "myName",
"location": "myLoc"
} ]
UPDATE
#Entity
#Data //lombok
public class A extends SuperClass{
private String name;
private String location;
}
#Entity
#Data
public class B extends SuperClass {
private String someData;
private String otherData;
#ManyToOne(optional = false, cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
private A a;
}
public class SuperClass implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id;
}
Both entitys have a simple repository interface which extend CrudRepository.
UPDATE II
Now if I add #RestResourece( exported = false ) tag after #ManyToOne tag, I get the A entity "exposed" and I can access the data. But now, doing a POST on my B entity, I can't access it anymore because B isn't found by Resource<B> anymore. Why is that so?

How to make dynamic join to tables in jpql query

I need to make dynamic joins to table as per the data in column.
e.g
table : to_dos
to_do_id | value | object_id | object_type
Now here object_type will be the table name and object_id will be primary key of that table, because my to_do table can have join with multiple table according to type of to_do
Now when I want to fetch to dos and its respective data from respective table I have following steps:
1-get todo data
2-get respective tables data in loop as per the object type
Can we do this without loop in JPQL, HQL or in Mysql? I need to avoid loop
Also please suggest if there is any more optimized way
One solutio would be to use polymorphic associations between the ToDo entity and the related ToDoTask entity types.
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
#DiscriminatorColumn(name = "TYPE")
public class Task {
#Id
#GeneratedValue
private Integer id;
#ManyToOne
private TaskList taskList;
#Column(name = "TYPE", insertable = false, updatable = false, nullable = false)
private String type;
}
#Entity
#DiscriminatorValue("TYPE1")
public class SubTaskType1 extends Task {}
#Entity
#DiscriminatorValue("TYPE2")
public class SubTaskType2 extends Task {}
#Entity
public class ToDoList {
#Id
#GeneratedValue
private Integer id;
#OneToMany(mappedBy = "taskList")
private List<Task> tasks;
}
Your code should then simply become:
ToDoList todos = todoRepository.findById( todoId );
for ( Task task : todos.getTasks() ) {
// access task.getType() to get the discriminator type
// or
// use instanceof to handle subclass casting
}