How do I prevent duplicate items in the cart? - java

For the current structure of database:
database structure
Users
#Entity
#Table(name = "users")
public class User {
#Id
#Column(name = "username", unique = true, nullable = false, length = 45)
private String username;
#Column(name = "password", nullable = false, length = 60)
private String password;
#Column(name = "enabled", nullable = false)
private boolean enabled;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade=CascadeType.ALL)
private Set<UserRole> userRole = new HashSet<UserRole>(0);
#ManyToMany(fetch=FetchType.EAGER)
#JoinTable(
name = "users_product",
joinColumns ={#JoinColumn (name = "users_username", referencedColumnName = "username")},
inverseJoinColumns = {#JoinColumn(name = "Products_idProducts", referencedColumnName = "idProducts")}
)
List<Products> productsList = new ArrayList<Products>();
Products
#Entity
#Table(name = "products")
public class Products implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "idProducts")
private long id;
#Column(name = "description")
private String description;
#Column(name = "price")
private String price;
#Column(name = "image")
private byte [] image;
#ManyToMany(mappedBy = "productsList")
List<User> usersList = new ArrayList<User>();
//setters - getters
I want to implement a shopping basket.
What I have now: A page with products and checkboxes on the view page. I find these products and send them to my controller, and afterwards set them to the current user.
productsList = myService.findManyProducts(toAdd);
user.setProductsList(productsList);
myService.updateUser(user);
#Transactional
public void updateUser(User user){
userDao.update(user);
}
update user function:
#Override
public void update(User user) {
entityManager.merge(user);
}
These actions are entered in the database as different values, depending on the latest additions.
What I want:
User adds some products to cart, browse other items, and add new products to the cart. How do I ensure that duplicate items are not found in the cart? Does JPA have some standard methods? Or do I need to check the product list by myself before adding them to the database?

I solved this by changing the List collection to a Set in the User entity.

Related

Trying to save via a Many To Many relationship in Java Spring Boot causes infinite loop

I have two tables. A Users table and an Artists table. Users can be associate with many artists, and vice versa. I have an API call that adds an Artist to the User's list. The API seems to work correctly, but when I check the User afterward, my postman return shows an endless list of the artist I added.
The user entity:
#Entity
#Table(name = "users")
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Slf4j
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", nullable = false)
Integer id;
#Column(name = "username")
String username;
#Column(name = "picture_link")
String pictureLink;
#ManyToMany
#JoinTable(
name = "user_artist",
joinColumns = #JoinColumn(name = "user_id"),
inverseJoinColumns = #JoinColumn(name = "artist_id"))
Set<Artist> artists = new HashSet<>();
The artist entity:
#Entity
#Table(name = "artists")
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#Slf4j
public class Artist {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id", nullable = false)
Integer id;
#Column(name = "name")
String name;
#Column(name = "description")
String description;
#Column(name = "picture_link")
String pictureLink;
#Column(name = "genres")
String genres;
#ManyToMany(mappedBy = "artists")
Set<User> users = new HashSet<>();
#Transient
List<Album> albums = new ArrayList<>();
}
The api call that causes the infinite loop:
#Override
public String addArtistToFaveList(int user_id, int artist_id) {
try{
User foundUser = new User();
Artist foundArtist = new Artist();
Optional<User> resultUser = userRepo.findById(user_id);
Optional<Artist> resultArtist = artistRepo.findById(artist_id);
if(resultUser.isPresent() && resultArtist.isPresent()){
foundUser = resultUser.get();
foundArtist = resultArtist.get();
}
Set<Artist> userFaveSet = foundUser.getArtists();
userFaveSet.add(foundArtist);
userRepo.save(foundUser);
return "THIS WORKED!";
}catch(Exception e){
return "Failed completely.";
}
}

role based creation of users in Spring boot

i have Three entities User, Institution and Role.
1)one to many between user and institution
2)and many to many between User and Role
-------user-------
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="user_Id")
private int userId;
#Column(name="name")
private String name;
#Column(name="lastname")
private String lastname;
#Column(name="email")
private String email;
#Column(name="password")
private String password;
#Column(name="isActive")
private boolean isActive;
#Column(name="lastActive")
private String lastActive;
#Column(name="createdDate")
private String createdDate;
#Column(name="isBlocked")
private boolean isBlocked;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "institution_id", nullable = false)
#JsonIgnoreProperties(value = {"user"})
private Institution institution;
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "user_has_role",
joinColumns = {
#JoinColumn(name = "user_id", referencedColumnName = "user_id",
nullable = false, updatable = true)},
inverseJoinColumns = {
#JoinColumn(name = "role_id", referencedColumnName = "role_id",
nullable = false, updatable = true)})
#JsonIgnoreProperties(value = {"users"})
private Set<Role> roles = new HashSet<>();
}
-------institution-------
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#ToString
#Entity
#Table(name = "institution")
public class Institution {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="institution_Id")
private int institutionId;
#Column(name="name")
private String name;
#Column(name="type")
private String type;
#Column(name="location")
private String location;
#OneToMany(mappedBy = "institution", fetch = FetchType.LAZY)
#JsonIgnoreProperties(value = {"institution" , "user"})
private Set<User> user;
}
-------role-------
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#ToString
#Entity
#Table(name = "role")
public class Role {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="role_Id")
private int roleId;
#Column(name="name")
private String name;
#Column(name="description")
private String description;
#ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
#JsonIgnoreProperties(value = {"roles"})
private Set<User> users = new HashSet<>();
}
Those are my 3 entities and tables in MySql
i have 7 roles
• Super-User
• Bank-Admin
• Bank-Support
• Bank-Service
• Merchant-Admin
• Merchant-Support
• Merchant-service
The super-User can create a user of any role
#PostMapping("/addUser")
public String addUser(#RequestBody User user) {
String rawpassword = user.getPassword();
String encodedpasswrod = passwordencoder.encode(rawpassword);
user.setPassword(encodedpasswrod);
userrepository.save(user);
return "user saved with name: " + user.getName();
}
this api works and i can set the role to anything in my api json body
But want that if the User is Bank-Admin he can only create Bank-Support and Bank-Service
im trying to create a new API which can only create a user with those 2 specific roles.
and then restrict the bank admin to access the other API that can create users of any kind.
is there any other way to do it and if no how can i do that...
You have to implement your custom implementation of User Entitlement.
Like according to login person, you will get that login person role, and according to your criteria just put validation like check that entity he is trying to add is he eligible to create it.
Map<String, List<String>> roleUserAccessMap = new HashMap<>();
roleUserAccessMap.put("Bank-Admin", Arrays.asList("Bank-Support", "Bank-Service"));
Just check like below
String loginPersonRole="Bank-Admin"; //This value should get from logged-in person context
if(roleUserAccessMap.containsKey(loginPersonRole) && roleUserAccessMap.get(loginPersonRole).contains(newuserrole) ){
//proceed ahead with Add api
}else{
System.out.println("You do not have enough privileage to create Use");
}
This will help you.

How should I get related entity?

I want to understand how to work with related entities in Hibernate.
There are two related entities:
#Entity
#Table(name = "usr")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(nullable = false)
private String username;
#Column(nullable = false)
private String password;
#Column(nullable = false)
private String email;
private boolean active;
#Enumerated(EnumType.STRING)
private Role role;
#OneToMany(fetch = FetchType.LAZY,
mappedBy = "responsibleUser", cascade = CascadeType.ALL)
private List<GrowBox> growBoxes;
//def-constructor , getters, setters
}
and
#Entity
#Table(name = "growBoxes")
public class GrowBox {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
#Column(nullable = false)
private Integer length;
#Column(nullable = false)
private Integer width;
#Column(nullable = false)
private Integer height;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "responsibleGrowBox", cascade = CascadeType.ALL)
private List<Plant> plants;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "activeGrowBox", cascade = CascadeType.ALL)
private List<Sensor> sensors;
#ManyToOne
#JoinColumn(name = "user_id")
private User responsibleUser;
//def-constructor , getters, setters
}
I have registered mapping using annotations. Hope it is correct. And I want to find box by user Id, but don't know how HQL query should be written. 'Cause there is no "user_id" field in my Box Class. Instead there is "User responsibleUser" field. And smth like this won't work(should not)
#Autowired
SessionFactory sessionFactory;
#Override
public List<GrowBox> findByUser(Long userId) {
Session session = sessionFactory.openSession();
String hqlQuery = "from GrowBox where user_id =: userId";
Query query = session.createQuery(hqlQuery);
List growBoxes = query.getResultList();
session.flush();
session.close();
return growBoxes;
}
A HQL query would be
String hqlQuery = "from GrowBox gb where gb.responsibleUser.id =: userId";

Problem with saving data in ManyToMany tables, hibernate, spring

Below - parent table - users:
#Entity
#Table(name = "user")
public class User {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "username", length = 50, unique = true)
#NotNull
#Size(min = 4, max = 50)
private String username;
#Column(name = "password", length = 100)
#NotNull
#Size(min = 4, max = 100)
private String password;
#ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
#JoinTable(
name = "user_role",
joinColumns = {#JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "role_id", referencedColumnName = "id")})
private List<Role> roles;
//constructor, getters/setters
Below You can find table with roles:
#Entity
#Table(name = "role")
public class Role {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "name", length = 50)
#NotNull
#Enumerated(EnumType.STRING)
private RoleName name;
#ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
private List<User> users;
//constructor, getters/setters
And I will try save User by belows method:
private static SessionFactory sessionFactory;
private Session session;
#Override
public void saveUser(User user) {
Role role = getRoleByName(RoleName.ROLE_ADMIN);
user.getRoles().add(role );
role.getUsers().add( user );
session.persist(user);
}
public Role getRoleByName(RoleName name) {
Query query = session.createQuery("SELECT r FROM Role r WHERE r.name= :name");
query.setParameter("name", name);
return (Role) query.uniqueResult();
}
But I have two problems:
- first - the main problem - I did something wrong and I can't save new user.
- second one - I have null result in quer result in getRoleByName, but of course, that role exists in table user_roles.
And any solution will be perfect for me.
Thank you in advance.

Fetch result in case of many to many relationship

I have three tables
employee: storing employee information
role_description: storing types of role i have like admin, zone manger, country manager
employee_role_details: having many to many relationship from employee and role description with some extra fields.
Now I am facing an issue in fetching result that contains (employee_id, email) from employee table, (role_ownership, role_id) from employee_role_details and (role_name and role_description)from role_description corresponding to role_id in role_description.
Employee.java
#Entity
#Table(name = "employee", uniqueConstraints= #UniqueConstraint(columnNames={"employee_email"}))
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "employee_id")
private long employeeId;
#Column(name = "employee_name")
private String employeeName;
#Column(name = "employee_email", unique = true, nullable = false)
private String employeeEmail;
#Column(name = "contact_no")
private String contactNo;
#Column(name = "password")
private String password;
#Column(name = "is_active")
private Boolean isActive = true;
#Transient
private long EmployeeRoleId;
#Transient
private String roleName;
#Transient
private int totalpage;
#OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
//#JsonIgnore
#JsonManagedReference(value = "employeeRoleRecord-employee")
#LazyCollection(LazyCollectionOption.FALSE)
private List<EmployeeRoleRecord> roleRecords = new ArrayList<>();
#OneToMany(mappedBy = "assignedTo", cascade = CascadeType.ALL)
#JsonManagedReference(value = "complaint-treatment-employee")
#LazyCollection(LazyCollectionOption.FALSE)
private List<ComplaintsCategory> complaintCategoryAssignedTo = new ArrayList<>();
EmployeeDescription.java
#Entity
#Table(name = "role_description")
public class RoleDescription {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "role_id")
private long roleId;
#Column(name = "role_name")
private String roleName;
#Column(name = "role_description")
private String roleDescription;
#Column(name = "status")
private boolean status;
#OneToMany(mappedBy = "description", cascade = CascadeType.ALL)
#JsonManagedReference(value = "employeeRoleRecord-roleDescription")
#LazyCollection(LazyCollectionOption.FALSE)
private List<EmployeeRoleRecord> roleRecords = new ArrayList<EmployeeRoleRecord>();
EmployeeRoleRecord
#Entity
#Table(name = "employee_role_record")
public class EmployeeRoleRecord {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "employee_role_id")
private long employeeRoleId;
#Column(name = "role_ownership")
private String roleOwnership;
#Column(name = "status")
private boolean status = true;
#ManyToOne(cascade=CascadeType.ALL)
#JsonBackReference(value = "employeeRoleRecord-employee")
#JoinColumn(name = "employee_id")
private Employee employee;
#ManyToOne(cascade=CascadeType.ALL)
#JsonBackReference(value = "employeeRoleRecord-roleDescription")
#JoinColumn(name = "role_id")
private RoleDescription description;
#Transient
private long roleId;
Code inside a dao
public Employee getEmployeeByEmail(String email) {
return (Employee) sessionFactory.getCurrentSession()
.createCriteria(Employee.class)
.add(Restrictions.eq("employeeEmail", email)).uniqueResult();
}
services:
public Employee getSurveyData(String employeeEmail){
Employee loggedInEmployee =
loginDao.getEmployeeByEmail(employeeEmail);
Employee loggedInEmployeeRecord = new Employee();
loggedInEmployeeRecord.setEmployeeId(loggedInEmployee.getEmployeeId());
loggedInEmployeeRecord.setEmployeeName(loggedInEmployee.getEmployeeName());
List<EmployeeRoleRecord> employeeRoleRecords = loggedInEmployee.getRoleRecords();
List<EmployeeRoleRecord> employeeRoleRecord = new ArrayList<>();
for(EmployeeRoleRecord record : employeeRoleRecords){
EmployeeRoleRecord employeeRole = new EmployeeRoleRecord();
employeeRole.setRoleId(record.getDescription().getRoleId());
employeeRole.setRoleName(record.getDescription().getRoleName());
employeeRole.setRoleOwnership(record.getRoleOwnership());
employeeRole.setStatus(record.isStatus());
employeeRoleRecord.add(employeeRole);
}
loggedInEmployeeRecord.setRoleRecords(employeeRoleRecord);
return loggedInEmployee;
}
Output:
{
"employeeId": 1,
"employeeName": "Dhyanandra Singh",
"employeeEmail": "admin",
"contactNo": "9893651872",
"password": "123456",
"isActive": true,
"roleRecords": [
{
"employeeRoleId": 1,
"roleOwnership": "overall project",
"status": true,
"roleId": 0,
"roleName": null
}
],
}
problem:
json output i'm getting is not containing role id and role name in role records list.
The problem is:
Your roleId attribute on EmployeeRoleRecord classe is mapped as #Transient, which means that it is not persisted on database, so Hibernate is not able to fetch the result of this field.
Looking at your classes modeling I couldn't figure out exactly what you are trying to map with the roleId, I suggest two possible approaches:
Do not return directly your query on getEmployeeByEmail method. First get the results, then iterate it and set manually the desired roleId you want on each record;
Check your model scheme and make the correct mapping for roleId, it probably should reference another entity, so make sure to map the relationship correctly to get your results on a single query.
Good luck.
added transient roleId and roleName in EmployeeRoleRecord then having some modification in services. like iterating list setting roleId and roleName in it.
public Employee getSurveyData(String employeeEmail){
Employee loggedInEmployee =
loginDao.getEmployeeByEmail(employeeEmail);
Employee loggedInEmployeeRecord = new Employee();
loggedInEmployeeRecord.setEmployeeId(loggedInEmployee.getEmployeeId());
loggedInEmployeeRecord.setEmployeeName(loggedInEmployee.getEmployeeName());
loggedInEmployeeRecord.setEmployeeEmail(loggedInEmployee.getEmployeeEmail());
loggedInEmployeeRecord.setContactNo(loggedInEmployee.getContactNo());
List<EmployeeRoleRecord> employeeRoleRecords = loggedInEmployee.getRoleRecords();
List<EmployeeRoleRecord> employeeRoleRecord = new ArrayList<>();
for(EmployeeRoleRecord record : employeeRoleRecords){
EmployeeRoleRecord employeeRole = new EmployeeRoleRecord();
employeeRole.setEmployeeRoleId(employeeRole.getEmployeeRoleId());
employeeRole.setRoleId(record.getDescription().getRoleId());
employeeRole.setRoleName(record.getDescription().getRoleName());
employeeRole.setRoleOwnership(record.getRoleOwnership());
employeeRole.setStatus(record.isStatus());
employeeRoleRecord.add(employeeRole);
}
loggedInEmployeeRecord.setRoleRecords(employeeRoleRecord);
return loggedInEmployeeRecord;
}

Categories