Persistence in hibernate and spring - spring

I have a question about Spring and Hibernate and that is, if for example I want to persist two entities at the same time, such as Customer and Address, that when a customer is registered, their address is automatically saved as well.
It is that the address is retrieved from an address api external to my application and I want that information to be saved in the Address entity when the client registers.
My entities are the following:
Customer :
public class Customer{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank(message = "no puede estar vació")
private String nombre;
#NotBlank(message = "no puede estar vació")
#Column(name = "apellido_paterno")
private String apellidoPaterno;
#NotBlank(message = "no puede estar vació")
#Column(name = "apellido_materno")
private String apellidoMaterno;
#NotBlank(message = "no puede estar vació")
private String telefono;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "address_id")
#JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
private Address address;
}
Address:
public class Address{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank(message = "no puede estar vació")
private String calle;
#NotBlank(message = "no puede estar vació")
private String colonia;
#NotNull(message = "no puede estar vació")
#Column(name = "no_exterior")
private Integer noExterior;
#Column(name = "no_interior")
private Integer noInterior;
#NotBlank(message = "no puede estar vació")
private String municipio;
#NotNull(message = "no puede estar vació")
private Integer cp;
#NotBlank(message = "no puede estar vació")
private String estado;
#OneToMany(mappedBy = "adress")
private List<Customer> customer;
}
Since I have it, the address must already be created so that it can be associated with the client, however, I want that when the client enters their data, including the address, they are created at the same time and the association is maintained.

When you persist the Entity "Customer" you should create a object Customer and a object Address, and set to the Customer the Address object and add to the Customer list in Address object, the object Customer. I mean, you should do it in your business logic layer, or your service.

Related

Can't get products by userId from repository

I have 2 tables. One of them called 'products'
#Data
#Entity
#Table(name = "products")
#NoArgsConstructor
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(length = 100)
#NotBlank(message = "Name must be written")
private String name;
#Column(length = 200)
#NotBlank(message = "Provide image (link in this case) of your product")
private String image;
#PositiveOrZero
private int amount;
#Column(length = 250)
#NotBlank(message = "description must be written")
#Size(min = 10, max = 250, message = "description is too long or empty")
private String description;
#PositiveOrZero
private float price;
#ManyToOne
#JoinColumn(name = "type_id")
private ProductType productType;
public Product(#NotBlank String name, String image, int amount, #NotBlank String description,
#PositiveOrZero float price, ProductType productType) {
this.name = name;
this.image = image;
this.amount = amount;
this.description = description;
this.price = price;
this.productType = productType;
}
}
another table is 'users'
#Data
#Entity
#NoArgsConstructor
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(length = 50)
#Size(min = 2, max = 30, message = "enter appropriate amount of letters, min 2")
private String username;
#Column(length = 100)
#Email(message = "Enter a valid email")
#NotBlank(message = "email should have a value")
private String email;
#Column(length = 50)
#NotBlank(message = "password should have a value")
#Size(min = 6, message = "password should at least consist of 6 characters")
private String password;
private boolean enabled;
private String role;
public User(#Size(min = 2, max = 30, message = "enter appropriate amount of letters, min 2")
String username,
#Email(message = "Enter a valid email")
#NotBlank(message = "email should have a value") String email,
#NotBlank(message = "password should have a value")
#Size(min = 6, message = "password should at least consist of 6 characters")
String password, boolean enabled, String role) {
this.username = username;
this.email = email;
this.password = password;
this.enabled = enabled;
this.role = role;
}
}
and also table that include both 'product_user' (many to many relationship)
it looks like this
#Data
#Entity
#Table(name = "product_user")
#AllArgsConstructor
#NoArgsConstructor
public class ProdAndUser{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
#ManyToOne
#JoinColumn(name = "product_id")
Product product;
#ManyToOne
#JoinColumn(name = "user_id")
User user;
public ProdAndUser(Product product, User user) {
this.product = product;
this.user = user;
}
}
then I tried to get them from prodAndUser repository by UserId or by User as obj:
#Repository
public interface ProdAndUserRepository extends JpaRepository<ProdAndUser, Integer> {
List<ProdAndUser> getProdAndUsersByUserId(Integer id);
List<ProdAndUser> getAllByUser(User user);
}
my controller looks like this:
#ResponseBody
#GetMapping("/findByUsr/{user}")
public List<ProdAndUser> getByUser(#PathVariable User user){
return prodAndUserRepository.getAllByUser(user);
}
error:
{
"timestamp": "2022-02-12T05:52:53.165+00:00",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/Cart/findByUsr"
}
I have tried to find them all by .findAll() and it worked fine. Also another tables work fine on their own
Look at the error it says (404) means something is not right with the path.
The path on the error does not contain user_id.

How can I retrieve all the children of a record in this Hibernate #ManyToOne relation?

I am working on a Spring Boot project using Spring Data JPA and Hibernate mapping. I have the following doubt about how can I implement the following query.
I have an User entity class like this:
#Entity
#Table(name = "portal_user")
#Getter
#Setter
public class User implements Serializable {
private static final long serialVersionUID = 5062673109048808267L;
#Id
#Column(name = "id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
#Column(name = "first_name")
#NotNull(message = "{NotNull.User.firstName.Validation}")
private String firstName;
#Column(name = "middle_name")
private String middleName;
#Column(name = "surname")
#NotNull(message = "{NotNull.User.surname.Validation}")
private String surname;
#Column(name = "sex")
#NotNull(message = "{NotNull.User.sex.Validation}")
private char sex;
#Column(name = "birthdate")
#NotNull(message = "{NotNull.User.birthdate.Validation}")
private Date birthdate;
#Column(name = "tax_code")
#NotNull(message = "{NotNull.User.taxCode.Validation}")
private String taxCode;
#Column(name = "e_mail")
#NotNull(message = "{NotNull.User.email.Validation}")
private String email;
#Column(name = "pswd")
#NotNull(message = "{NotNull.User.pswd.Validation}")
private String pswd;
#Column(name = "contact_number")
#NotNull(message = "{NotNull.User.contactNumber.Validation}")
private String contactNumber;
#Temporal(TemporalType.DATE)
#Column(name = "created_at")
private Date createdAt;
#Column(name = "is_active")
private boolean is_active;
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
#JsonManagedReference
private Set<Address> addressesList = new HashSet<>();
#ManyToMany(cascade = { CascadeType.MERGE })
#JoinTable(
name = "portal_user_user_type",
joinColumns = { #JoinColumn(name = "portal_user_id_fk") },
inverseJoinColumns = { #JoinColumn(name = "user_type_id_fk") }
)
Set<UserType> userTypes;
#ManyToOne(fetch = FetchType.EAGER)
#JsonProperty("subagent")
private User parent;
public User() {
super();
}
public User(String firstName, String middleName, String surname, char sex, Date birthdate, String taxCode,
String email, String pswd, String contactNumber, Date createdAt, boolean is_active) {
super();
this.firstName = firstName;
this.middleName = middleName;
this.surname = surname;
this.sex = sex;
this.birthdate = birthdate;
this.taxCode = taxCode;
this.email = email;
this.pswd = pswd;
this.contactNumber = contactNumber;
this.createdAt = createdAt;
this.is_active = is_active;
}
}
The instances of this class represents users of my system. An user can have a single specific parent (the concept is similar to that of a referral: an user can bring another user in the system). This is handled by this ManyToOne recursive relationship:
#ManyToOne(fetch = FetchType.EAGER)
#JsonProperty("subagent")
private User parent;
Basically an user contains is parent (who bring him\her into the platform). It works fine. So retrieving an user I can easily retrieve the information of who is its parent (it is contained into the retrieved User object).
Now I need to implement the inverse behavior: I have to define a "query" that starting from a parent retrieve all its children.
The previous User entity class maps the following DB table:
The highlighter parent_id contains the FK that define this recursive relationship. So it contains the PK of another user that is the parent.
I have this UserRepository repository interface (it extents the JpaRepository interface)
public interface UsersRepository extends JpaRepository<User, Integer> {
User findByemail(String email);
List<User> findByUserTypes_TypeName(String typeName);
}
As you can see I am using a "query by method" style. Is it possiblem implement a behavior like this using "query by method" style? (in case also JPQL could be fine)
You can do this
List<User> findByParent_Id(Integer id);
Or you can do this
#Query("SELECT u FROM User u WHERE u.id = ?1")
List<User> getReferredUsers(Integer id);
The relationship between the user and the parent is unidirectional in the given code. By making it bidirectional, it is easy to query the data in either ways.
Refer to below code to make it bidirectional. Also ensure the relevant FetchType to avoid the performance risk. Here FetchType.LAZY is used for one to many association so it queries the data using the proxy reference when needed.
#ManyToOne(fetch = FetchType.EAGER)
#JsonProperty("subagent")
#JsonBackReference
private User parent;
#JsonManagedReference
#OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
private Set<User> userSet = new HashSet<>();
Child entities are fetched only when parent.getUserSet is used because of the FetchType.Lazy
public Set<User> getUsers(int id) {
User parent = userRepository.getById(id);
return parent.getUserSet();
}

Spring Boot as API Rest is not deserializing the entire object

Im using Spring Boot as API Rest with VueJs on the frontend.
My models classes are:
#EqualsAndHashCode(callSuper = true)
#Entity
#Data
public class Habitante extends Persona {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#NotBlank
#Pattern(regexp = "^[HM]$")
private String sexo;
#NotBlank
private String nacionalidad;
#Valid
#ManyToOne
#JsonIdentityReference(alwaysAsId = true)
private Vivienda Vivienda;
#Valid
#ManyToOne
#JsonIdentityReference(alwaysAsId = true)
private Identificacion tarjetaIdentificacion;
#Column(unique = true)
private String identificacion;
}
and
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Solicitud implements Serializable {
private static final long serialVersionUID = 9178661439383356177L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#Past
private Date fecha;
#Valid
#ManyToOne
private Habitante solicitante;
private String justificacion;
#Pattern(regexp = "^[AM]$")
#NotBlank
private String tipo;
#Pattern(regexp = "^(ACR|AIM|MV|MD|MRE)$")
#NotBlank
private String subtipo;
// El estado puede ser pendiente (P), aceptada (A), rechazada (R) y cancelada (C)
#NotBlank
#Pattern(regexp = "^[ACRP]$")
private String estado;
// Sólo para menores de edad que lo quieran añadir o para personas con tarjeta identificativa
#Pattern(regexp = "^(\\d{8}\\w)|(\\d{7}[XYZ])$")
private String identificacion;
#Valid
#ManyToOne
private Identificacion tipoIdentificacion;
private String nombre;
private String primerApellido;
private String segundoApellido;
#Valid
#ManyToOne
private Vivienda vivienda;
#Past
private Date fechaNacimiento;
#Valid
#OneToMany
private List<Documento> documentos;
}
When I tried to send a new Solicitud using axios like this:
const solicitud = {
fecha: new Date(),
solicitante: {
id: props.userLogged.id
},
estado: 'P',
justificacion: '',
tipo: opcion.value,
subtipo: subOpcion.value,
nombre: nombre.value,
primerApellido: primerApellido.value,
segundoApellido: segundoApellido.value,
fechaNacimiento: fechaNacimiento.value,
tipoIdentificacion: { id: tipoIdentificacion },
// identificacion: tIdentificacion,
documentos: [],
};
await axios.post(`${BASE_URL}solicitud/habitante/new`, solicitud);
#PostMapping(value = "/habitante/new")
public Respuesta nuevaSolicitud(#RequestBody Solicitud solicitud){ ... }
On Spring, I dont get the entire Habitanteobject, only the id, the same thing happens with the property vivienda on another request, I get this:
Is there anyway to get the entire object on the controller?

Jpa-Jpql Query to Select All field from parent Entity and specific field from child Entity in OneToOneMapping

I have oneToOne RelationShip between Employee and student entity i want to fetch all field from Employee Entity and name and modelNumber From Laptop Entity
Employee Entity
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "employee_name")
private String name;
#Column(name = "date_of_birth")
private String dob;
#Column(name = "gender")
private char gender;
#Column(name = "skills")
private String[] skills;
#OneToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
#JoinColumn(name = "laptop_id")
private Laptop laptop;
//getter setter
Laptop Entity
#Entity
public class Laptop {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String ram;
private String modelNumber;
private String processor;
//getter Setter
select e.id, e.name, e.dob, e.gender, e.skills,
e.laptop.name, e.laptop.modelNumber
from Employee e
Please start by reading the docs: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql

Insert in child and parent JSON Spring Boot

I have 3 entities in my spring boot App data rest, Appusers, Teacher, and Student
Appusers
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name= "appuserId", updatable=false)
private Long appuserId;
#Column(name = "username")
private String username;
#Column(name = "fullname")
private String fullName;
#Column(name = "email")
private String email;
#Column(name = "password")
private String password;
#OneToOne(mappedBy = "appuser")
private Teacher teacher;
#OneToOne(mappedBy = "appuser")
private Student student;
Teacher
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name = "teacherId" , updatable = false)
private Long teacherId;
#Column(name= "firstname")
private String firstName;
#Column(name = "lastname")
private String lastName;
#Column(name="designation")
private String designation;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "appuserId", nullable = true)
private Appuser appuser;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval=true ,mappedBy="teacher")
#JsonIgnore
private List<Course> courses;
Student
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name= "studentId", updatable=false)
private Long studentId;
#Column(name = "firstName")
private String firstName;
#Column(name = "lastName")
private String lastName;
#Column(name = "enrolledSince")
private String enrolledSince;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "appuserId", nullable = false)
private Appuser appuser;
#OneToMany(cascade = CascadeType.ALL,orphanRemoval=true, mappedBy="student")
#JsonIgnore
private List<CourseStudent> courseStudents;
i can insert in appusers table using json format in postman and it goes well. but when i try to insert in teacher or student table the result in appusers is null. it shouldnt be null because teacher and student foreign key to appusers.
This should not happen. When you save a teacher or a student you should specify appuser which is already in the database. And use appuserId instead Appuser, which is quite enough to identify to which appuser it belongs.
You can get your appuser after you save a teacher or a student and do request with join to the database.
when you try to insert in teacher or student table, please make sure that you are setting the value to appuser while persisting.
Student std =new Student();
// create an object of appuser,set its vaue and assign it to student object
Appuser ap = new Appuser();
// assigning values to the appuser object as ap.setfullname="...";... so on
std.setAppuser=ap;
now persist this student object the entries will be reflected in the mapped table
or you can set the id of appuser in std object that is already persisted .

Resources