I've got a spring mvc application in which users book meeting rooms. When I try to delete a booking by using deleteById(), the user that belongs to the booking is also deleted. How do I prevent this?
Booking object:
package spe_booker.models;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
#Entity
public class Booking {
#Id
#GeneratedValue
private Long id;
#NotNull
private Date startDateTime;
#NotNull
private Date endDateTime;
#NotNull
#ManyToOne(cascade = CascadeType.ALL)
private Room room;
#NotNull
#ManyToOne(cascade = CascadeType.ALL)
private User user;
private Date creationDate;
public Booking() { }
...getters and setters...
User object:
package spe_booker.models;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue
private Long id;
private String username;
String name;
private String password;
private String faculty;
private String role;
private Boolean blacklisted;
#Column(name = "enabled")
public int enabled;
#OneToMany(mappedBy = "user")
private List<Booking> bookings = new ArrayList<>();
...getters and setters...
Booking repository:
package spe_booker.Repositorys;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import spe_booker.models.Booking;
import spe_booker.models.User;
import java.util.List;
#Repository
public interface BookingRepository extends CrudRepository<Booking, Long> {
List<Booking> findAll();
List<Booking> findBookingsByUser(User user);
}
The delete call:
bookingRepository.deleteById(id);
Change the cascade of
#NotNull
#ManyToOne(cascade = CascadeType.ALL)
private User user;
to the appropriate values. Do you change a user and then save a booking and a user at the same time? Do you create bookings with users at the same time? If you can answer these questions with "no", you might just remove (cascade = CascadeType.ALL) at all. And you should ask yourself why the cascades are present in the first place!
CascadeType.ALL forces JPA to cascade all operations (persisting, removing, updating....). That means when you delete/update a booking, it willl delete/update the Room and User objects too
Related
Please I need some help. I've got 2 entities:
Appointment.class
#Entity
#Table(name = "appointment")
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Appointment {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "created_date")
private Date createdDate;
#Column(name = "modified_date")
private Date modifiedDate;
#Column(name = "appointment_date")
private LocalDate appointmentDate;
#Column(name = "start_time")
private LocalTime startTime;
private Boolean cancelled;
#ManyToOne
#JoinColumn(nullable = false, name = "client_id")
private Client clientId;
#ManyToOne
#JoinColumn(nullable = false, name = "employee_id")
private Employee employee;
#ManyToOne
#JoinColumn(nullable = false, name = "service_id")
private Service service;
}
And Employee.class
#Entity
#Table(name = "employee")
#NoArgsConstructor
#AllArgsConstructor
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "created_date")
private Date createdDate;
#Column(name = " modified_date")
private Date modifiedDate;
#OneToOne
#JoinColumn(name = "service_id", referencedColumnName = "id")
private Service service;
}
I need to get all the appointments that match with the given startTime, appointmentDate and employee
I want to define an abstract method in the interface AppointmentRepo so that in my AppointmentServices.class I can call that method with 3 arguments and get the appointment entity.
AppointmentServices.class
appointmentRepo.getAppointmentByDateAndEmployee(date, employee, scheduledHour);
AppointmentRepo interface
#Repository
public interface AppointmentRepo extends JpaRepository<Appointment, Integer>{
#Query("SELECT a FROM Appointment a INNER JOIN a.employee e WHERE a.appointmentDate = :appointment_date AND e = :employee AND s.startTime = :start_time")
public List<Appointment> getAppointmentByDateAndEmployee (#Param("appointment_date") LocalDate appointmentDate,
#Param("employee_id") Employee employee, #Param("start_time") LocalTime startTime);
}
How I have to set my #Query in order to be given an appointment entity that matches with 3 given arguments (a date, and time and a reference to other entity called Employee)
Am I doing wrong matching the entire object so I need just to use the id of the Employee entity?
Please help me, and thanks for your time!!
Happy Holidays
You can use SQL instead HQL (nativeQuery=true)
DAO Layer
package com.jb.app.repos;
import com.jb.app.beans.Appointment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
#Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Integer> {
#Query(value = "SELECT * FROM APPOINTMENT WHERE appointment_date = ?1 AND start_time = ?2 AND employee_id = ?3", nativeQuery = true)
List<Appointment> getAppointmentByDateAndEmployee(LocalDate appointmentDate, LocalTime startTime, int employeeId);
}
Service Layer
package com.jb.app.services;
import com.jb.app.beans.Appointment;
import com.jb.app.beans.Employee;
import com.jb.app.repos.AppointmentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
#Service
#RequiredArgsConstructor
public class AppointmentServiceImpl implements AppointmentService{
private final AppointmentRepository appointmentRepository;
#Override
public List<Appointment> getAppointmentByDateAndEmployee(LocalDate appointmentDate, LocalTime startTime, Employee e) {
return appointmentRepository.getAppointmentByDateAndEmployee(appointmentDate,startTime,e.getId());
}
}
I have the following code, where I need to populate a set with multiple objects that are instances of the same class (User). The problem is, I only get the first object when I log.
user = User.builder()
.id(1L)
.username("2397047")
.nickname("test1")
.build();
anotherUser = User.builder()
.id(2L)
.username("23971948")
.nickname("test2")
.build();
Set<User> userSet = new HashSet<>();
userSet.add(user);
userSet.add(anotherUser);
System.out.println("User set from test " + userSet);
This code produces the following output
User set from test [User(id=1, nickname=test1, username= 2397047, password=null, roles=null, groups=null)]
Why am I unable to get the entire collection?
This is my User class
package com.chama.chamaservice.user;
import com.chama.chamaservice.BaseEntity;
import com.chama.chamaservice.Views;
import com.chama.chamaservice.config.ApplicationUserRole;
import com.chama.chamaservice.group.Group;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.*;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
#Builder
#Entity
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#ToString
public class User extends BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#JsonView(Views.Public.class)
private Long id;
#JsonView(Views.Public.class)
private String nickname;
#JsonView(Views.Public.class)
private String username; // <- Unique user's phone number
private String password;
#ElementCollection(targetClass = ApplicationUserRole.class)
#CollectionTable(name = "user_role", joinColumns = #JoinColumn(name = "user_id"))
#Enumerated(EnumType.STRING)
#Column(name = "role")
private Set<ApplicationUserRole> roles;
#JsonIgnore
#ManyToMany(mappedBy = "groupMembers", fetch = FetchType.LAZY, targetEntity = Group.class)
private Set<Group> groups = new HashSet<>();
}
In the User class, #Data annotation which will implement #Getter, #Setter, #ToString method.
It will print all values in the Set.
Found an answer, although it may not be the optimal solution. I annotated the User class with #EqualsAndHashCode
I have problem with adding users role during registration request
This is my DB diagram:
And I have the following classes: first is entity role:
package application.model;
import lombok.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.persistence.*;
import java.util.List;
#NamedQueries({
#NamedQuery(name = User.GET_USERS, query = User.QUERY_GET_USERS),
})
#Getter
#Setter
#NoArgsConstructor
#Entity
#Table(name = "users")
public class User {
public static final String GET_USERS = "User.get_users";
public static final String QUERY_GET_USERS = "select u from User u";
#Id
#NotNull
#GeneratedValue(strategy = GenerationType.SEQUENCE)
#Column(name = "id")
public int id;
#NotNull
#NotEmpty
#Column(name="firstname")
private String firstname;
#NotNull
#Column(name="lastname")
private String lastname;
#NotNull
#Column(name="email")
private String email;
#NotNull
#Column(name="password")
private String password;
#JoinTable
#OneToMany
private List<Role> roles;
}
second entity is Role:
package application.model;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
#Getter
#Setter
#NoArgsConstructor
#Entity
#Table(name = "role")
public class Role {
#Id
#NotNull
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
public int id;
#NotNull
#Column(name="name")
private String name;
}
So, i have 3 roles in my application, when I register new user with post request:
{
"firstname": "imasdie5",
"lastname": "nazasdwisko5",
"email": "masdil",
"password": "pass",
"roles": [
{ "id":2 }
]
}
First user is registered correctly, but when i send second request with same roles.id=2, i have:
ERROR: duplicate key value violates unique constraint "uk_d9najy24fium4vkivgwjuf0hw"
Detail: Key (roles_id)=(2) already exists.
In dbeaver table users_role have constraint uk_d9najy24fium4vkivgwjuf0hw with type UNIQUE_KEY, so that's the problem, but how to change type to non-unique? Many users may have same role, so it is necessary for me
All tables are generated with hibernate.
Table users_role is not entity in my application, maybe it should be entity?
Do You have any advice what I should change to add one role for many users?
Try to use #ManyToMany annotation
#JoinTable
#ManyToMany
private List<Role> roles;
I want to generate User object using Thymeleaf template. How can I do this? I have an User class containing information as follow.
package com.rhv.um.model;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "firstname")
private String firstName;
#Column(name = "lastname")
private String lastName;
#Column(name = "email")
private String email;
#Column(name = "mobile")
private Long mobile;
#Column(name = "username")
private String username;
#Column(name = "password")
private String password;
#Transient
private String confirmPassword;
#Column(name = "isenable", columnDefinition = "boolean default 1")
private boolean isEnable;
#Column(name = "reason")
private String reason;
#ManyToMany
#JoinTable(name = "users_roles", joinColumns = #JoinColumn(name = "user_id"), inverseJoinColumns = #JoinColumn(name = "roles_id"))
private Set<Role> roles;
//Getter and Setter
}
I have an UserRepository class and UserService class too. UserService class is as follow. UserRepository class is same as UserService class
package com.rhv.um.service;
import java.util.List;
import com.rhv.um.model.User;
public interface UserService {
void save(User user, boolean updatePassword);
List<User> findAll();
User findByUsername(String username);
}
Now I want to create an object of User from thymeleaf by using findByUsername(String username) method of UserService class. I want to get the details of currently logged-in user from this.
As I am using SpringSecurity5. I can get currently logged in user from thymeleaf as below.
<span th:text="${#authentication.getPrincipal().getUsername()}"></span>
I want to pass this username to my methods and get the User object. Not only the currently logged-in user. I do need to get User object of other users in the system.
I know few ways of doing it as below.
Setting the values of currently logged-in user in cookie and fetch it from UI.
Setting the values of currently logged-in user in session and fetch from UI.
Setting the User object to model attribute and then use the object from UI.
Using above ways, My purpose is getting solved. But I want to set object from thymeleaf. How can I do it?
I had tried by using th:with of thymeleaf. It didn't worked for me
<div th:if="${userCredentials == null}" th:with="userCredentials=${T(your.package.here.UserCredentials).create()}"></div>
Post for above line of code is here
Please let me know if more information is needed. Also guide me with the code if I am doing anything wrong here.
The problem is that when i run the application hibernate creates a column user_id in the tahograph_cards table which i don't want and it doesn't create a column tahograph_card_id in the users table which I want to.
It's a OneToMany relationship.
Here are my files:
application.properties:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/getmydrivercard
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
User:
package com.getmydrivercard.getmydrivercard.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
#Entity
#Table(name = "users")
public class User {
private static final String EMPTY_STRING = "";
#Id
#GeneratedValue(generator = "uuid2")
#GenericGenerator(name = "uuid2", strategy = "uuid2")
#Column(columnDefinition = "BINARY(16)")
private UUID userId;
#NotEmpty
private String username;
#NotEmpty
private String password;
#NotEmpty
private String email;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
#OnDelete(action = OnDeleteAction.CASCADE)
#JsonIgnore
private Set<TahographCard> tahographCards;
private String comments;
protected User(){}
public User(String username, String password, String email){
setUsername(username);
setPassword(password);
setEmail(email);
setComments(EMPTY_STRING);
setTahographCards();
}
//getters and setters
}
TahographCard:
package com.getmydrivercard.getmydrivercard.models;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.util.UUID;
#Entity
#Table(name = "tahograph_cards")
public class TahographCard {
#Id
#GeneratedValue(generator = "uuid2")
#GenericGenerator(name = "uuid2", strategy = "uuid2")
#Column(columnDefinition = "BINARY(16)")
private UUID tahographCardId;
#NotEmpty
private Boolean isActive;
#NotEmpty
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userId")
private User user;
protected TahographCard(){}
public TahographCard(User user){
setUser(user);
setActive(true);
}
//getters and setters
}
I tried with mappedBy and some other stuff I found on the Internet but still doesn't work. Thank you in advance.
The problem here is not the entities configuration but the database design.
You can't map a oneToMany relationship with a FK column on the one side.
For example if you have 2 Users and 4 TahographCard (2 for each user) normally it would look like this:
User
id name
1 Foo
2 Bar
Table
id user_id (fk)
1 1
2 1
2 2
2 2
The opposite with the TahographCardId on the User table is impossible