I have 4 tables user, address, country password and right now spring boot for some reason is creating crazy multiple queries and for now country table is keep repeating itself when i tried to get result from findByEmail.
/**
* @author Marcel Zacharias
*
*/
package com.services.servicesapp.entity;
import java.io.Serializable;
import java.util.Date;
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.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "country")
@NoArgsConstructor
@Getter
@Setter
public class Country implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "country")
@NotNull
private String name;
@Temporal(value = TemporalType.TIMESTAMP)
@CreationTimestamp
@Column(name = "CREATED_TIME")
private Date creationTime;
@Temporal(value = TemporalType.TIMESTAMP)
@UpdateTimestamp
@Column(name = "UPDATED_TIME")
private Date updatedTime;
@OneToMany(mappedBy = "country")
private Set<Address> address;
}
User Table:
/**
* @author Marcel Zacharias
*
*/
package com.services.servicesapp.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "user",
uniqueConstraints = {@UniqueConstraint(name = "userEmail", columnNames = "email")}
)
@Getter
@Setter
@NoArgsConstructor
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(length = 5)
@NotBlank
private String title;
@NotBlank
@Size(min = 3)
private String firstName;
@NotBlank
@Size(min = 3)
private String lastName;
@NotBlank
@Email
private String email;
private String phone;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "image_id", referencedColumnName = "id")
private Image image;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(
name = "services_used",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id")
)
private Set<ServiceCategory> usedServices;
@OneToMany(mappedBy = "user")
private Set<Complains> complains;
@NotBlank
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "password_id", referencedColumnName = "id")
private Password password;
@Column(columnDefinition = "boolean default false")
private Boolean enabled;
@Column(name = "verification_code")
private String verificationCode;
@Temporal(value = TemporalType.TIMESTAMP)
@CreationTimestamp
@Column(name = "CREATED_TIME")
private Date creationTime;
@Temporal(value = TemporalType.TIMESTAMP)
@UpdateTimestamp
@Column(name = "UPDATED_TIME")
private Date updatedTime;
public User(String title, String firstName, String lastName, String phone, String email, Address address,
Password password) {
this.title = title;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.address = address;
this.password = password;
}
public String getFullName() {
return String.format("%s %s %s", getTitle(), getFirstName(), getLastName());
}
}
Address table
/**
* @author Marcel Zacharias
*
*/
package com.services.servicesapp.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "address")
@NoArgsConstructor
@Getter
@Setter
public class Address implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "address1")
@NotBlank
private String address1;
@Column(name = "address2")
private String address2;
@Column(name = "city")
@NotBlank
private String city;
@Column(name = "postcode")
@NotBlank
private String postcode;
@Column(name = "region")
@NotBlank
private String region;
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "CREATED_TIME")
@CreationTimestamp
private Date creationTime;
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "UPDATED_TIME")
@UpdateTimestamp
private Date updatedTime;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "country_id", referencedColumnName = "id")
@NotBlank
private Country country;
@OneToOne(mappedBy = "address")
private User user;
public Address(String address1, String address2, String city, String postcode, String region,
Country country) {
this.address1 = address1;
this.address2 = address2;
this.city = city;
this.postcode = postcode;
this.region = region;
this.country = country;
}
}
Password Table:
/**
* @author Marcel Zacharias
*
*/
package com.services.servicesapp.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Table(name = "password")
@NoArgsConstructor
@Getter
@Setter
public class Password implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToOne(mappedBy = "password")
private User user;
@NotBlank
@Column(nullable = false)
private String password;
@Column(name = "temporary_password")
private String temporaryPassword;
@Temporal(value = TemporalType.TIMESTAMP)
@CreationTimestamp
@Column(name = "CREATED_TIME")
private Date creationTime;
@Temporal(value = TemporalType.TIMESTAMP)
@UpdateTimestamp
@Column(name = "UPDATED_TIME")
private Date updatedTime;
public Password(String password, String temporaryPassword) {
this.password = password;
this.temporaryPassword = temporaryPassword;
}
}
User Repository:
package com.services.servicesapp.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import com.services.servicesapp.entity.User;
@Repository
public interface UserRepository extends JpaRepository<User, Long>
{
Optional<User> findByEmail(String email);
Optional<User> findById(long id);
Optional<User> findByVerificationCode(String verificationCode);
}
Controller:
/**
* @author Marcel Zacaharias
*/
package com.services.servicesapp.controller;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;import javax.validation.Valid;
import com.services.servicesapp.DTO.user.RegistrationUserDTO;
import com.services.servicesapp.DTO.user.UserDTO;
import com.services.servicesapp.component.Mapper;
import com.services.servicesapp.component.helpers.Error;
import com.services.servicesapp.component.helpers.Success;
import com.services.servicesapp.entity.User;
import com.services.servicesapp.repository.UserRepository;
import com.services.servicesapp.service.EmailService;
import com.services.servicesapp.service.RegistrationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@RestController
@RequestMapping("registration")
@CrossOrigin(origins = "http://localhost", allowCredentials = "true")
@Slf4j
@RequiredArgsConstructor
public class RegistrationController {
private final RegistrationService service;
private final Mapper mapper;
private final EmailService emailService;
@Value("${app.frontend.url}")
private String url;
private final UserRepository userRepository;
@GetMapping("/test/getUserByEmail/{email}")
public ResponseEntity<User> test(@PathVariable String email) {
User user = userRepository.findByEmail(email).get();
// UserDTO transfer = mapper.userToDTOUser(user);
return ResponseEntity.ok().body(user);
}
}
and from postman just for testing i'm getting repeating address
{
"id": 2,
"title": "Mr",
"firstName": "Marcel",
"lastName": "Zetek",
"email": "test@gmail.com",
"phone": null,
"address": {
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
"postcode": "CM4 5NH",
"region": "Essex",
"country": {
"id": 1,
"name": "United Kingdom",
"address": [
{
"id": 2,
"address1": "12 southend rd",
"address2": null,
"city": "Chelmsford",
what do i have wrong in my relationships that the table keeps repeat itself and it created over 8k lines of code lol
can someone explain and correct please.