Spring Data JPA Specification Manyto One relationship - spring-data-jpa

I have two entities in Spring Data JPA:
User--->* TaxPayment
The goal is to get the all the taxpayments related to user_id:
User.java
public class User extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
..........
}
TaxPayment.jva
public class TaxPayment implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
#Column(name = "payment_date", nullable = false)
private LocalDate paymentDate;
#NotNull
#Column(name = "amount", precision=10, scale=2, nullable = false)
private BigDecimal amount;
#Column(name = "reference")
private String reference;
#ManyToOne
private User user;
public Long getId() {
return id;
}..
}
I dont wan't to have oneTOMany Annotation from User.java and the column mapped in the Taxpayment as user_id.
Specification class as follows:
final class TaxPaymentSpecification {
private TaxPaymentSpecification(){
}
static Specification<TaxPayment> hasUser(Long userId){
return new Specification<TaxPayment>() {
#Override
public Predicate toPredicate(Root<TaxPayment> arg0, CriteriaQuery<?> arg1,
CriteriaBuilder arg2) {
// TODO Auto-generated method stub
Root<TaxPayment> root = arg0;
Subquery<Long> subqry = arg1.subquery(Long.class);
Root<User> user = subqry.from(User.class);
final Join<User,TaxPayment> taxpays = root.join("user");
subqry.select(taxpays.<Long> get("user_id"));
subqry.where(arg2.equal(user.<Long> get("id"),userId));
return arg2.in(arg0.get("user_id")).value(subqry);
}
};
}
}
Is specification is correct or wrong as per my goal to get all the TaxPayment related to user_id?

There is no need to use subquery if you already know the userId, which will be the value of user_id in TaxPayment table:
#Override
public Predicate toPredicate(Root<TaxPayment> arg0, CriteriaQuery<?> arg1,
CriteriaBuilder arg2) {
return arg2.equal(arg0.get("user_id"), userId);
}

Related

#Transactional in spring JPA

I have a spring boot application where I need to update a migratedCustomer db table based on userId and phoneNumber.
Since I have to use for loop in the service layer for every update, it is creating a
new transaction and performance is hampered.
how could I make sure only one transaction is created and hence to improve the performance. code is like below
#Entity
#Table(name = "MigratedCustomer")
public class MigratedCustomer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String userId;
private String phoneNumber;
#Temporal(TemporalType.TIMESTAMP)
private Date createdTimestamp;
private int batchNumber;
private String comment;
}
public class MigratedCustomerService {
#Autowired
private UserRepository userRepository;
public void updateMsisdn(List<MigratedCustomer> savedCustomers) {
for (MigratedCustomer savedCustomer : savedCustomers) {
userRepository.updateStatus(savedCustomer.getUserId(),
savedCustomer.getPhoneNumber());
}
}
}
public interface MsisdnRepository extends JpaRepository<Msisdn, Long> {
#Modifying
#Query(value = "UPDATE Msisdn SET status=INACTIVE where userId=:userId and phoneNumber=:phoneNumber",
nativeQuery = true)
void updateStatus(#Param("userId") String userId, #Param("phoneNumber") String phoneNumber);
}

Spring Boot : Building object Rest API

I created 4 classes for 3 tables. Those tables are built to store orders with products.
My first entity is orders :
#Entity
#Table(name="orders")
public class OrderEntity implements Serializable {
...
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
private UserEntity seller;
private String paymentMode;
private double totalAmount;
#OneToMany(mappedBy = "pk.order")
private List<OrderProductEntity> orderProducts;
#DateTimeFormat
private Date createdAt;
...
}
My second entity is products:
#Entity
#Table(name="products")
#Getter #Setter
public class ProductEntity implements Serializable {
...
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
#Column(nullable = false)
private String productKeyId;
#ManyToOne
#JoinColumn(name = "category_id")
private CategoryEntity category;
#Column(nullable = false)
private String name;
#Column(nullable = false)
private double price;
#Column(nullable = false)
private int availableQty;
#JsonManagedReference
#OneToMany(mappedBy = "pk.product", fetch = FetchType.EAGER)
#Valid
private List<OrderProductEntity> orderProducts;
...
}
I also define the pivot table in the OrderProductEntity:
#Entity
#Table(name="order_product")
#Getter #Setter
public class OrderProductEntity {
#EmbeddedId
#JsonIgnore
private OrderProductPK pk;
#Column(nullable = false)
private Integer quantity;
// default constructor
public OrderProductEntity(){}
public OrderProductEntity(OrderEntity order, ProductEntity product, Integer quantity) {
pk = new OrderProductPK();
pk.setOrder(order);
pk.setProduct(product);
this.quantity = quantity;
}
}
And I defined the PK of this table :
#Embeddable
#Getter #Setter
public class OrderProductPK implements Serializable {
#JsonBackReference
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#JoinColumn(name = "order_id")
private OrderEntity order;
#ManyToOne(optional = false, fetch = FetchType.LAZY)
#JoinColumn(name = "product_id")
private ProductEntity product;
}
Now my issue is to return the data to the front office by a web service Rest.
I created a model that represent what i want to obtain as result.
public class OrderRest {
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<ProductRest> orderProducts;
private UserRest seller;
private Date createdAt;
}
And :
public class ProductRest {
private String productKeyId;
private String name;
private double price;
private int qty;
private String imgPath;
private CategoryRest category;
}
In my controller i want to return an OrderRest object :
public List<OrderRest> getOrders() {
List<OrderRest> returnValue = new ArrayList<>();
List<OrderDto> orderDtos = orderService.getOrders();
// loop the result
for (OrderDto orderDto : orderDtos) {
OrderRest orderRest = new OrderRest();
ModelMapper modelMapper = new ModelMapper();
orderRest = modelMapper.map(orderDto, OrderRest.class);
returnValue.add(orderRest);
}
return returnValue;
}
THis method map the List of OrderRest and call the service layer :
public List<OrderDto> getOrders() {
List<OrderDto> returnValue = new ArrayList<>();
Iterable<OrderEntity> orderEntities = orderRepository.findAll();
ModelMapper modelMapper = new ModelMapper();
for(OrderEntity orderEntity: orderEntities) {
OrderDto orderDto = new OrderDto();
UserDto userDto = orderDto.getSeller();
List<OrderProductDto> orderProductDtos = orderDto.getOrderProducts();
orderDto = modelMapper.map(orderEntity, OrderDto.class);
returnValue.add(orderDto);
}
return returnValue;
}
With OrderDto :
public class OrderDto implements Serializable {
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private Long id;
private String orderKeyId;
private String paymentMode;
private double totalAmount;
private List<OrderProductDto> orderProducts;
private UserDto seller;
private Date createdAt;
}
And OrderProductDto :
public class OrderProductDto implements Serializable {
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
private static final long serialVersionUID = 1L;
private String productKeyId;
private String name;
private Integer price;
private Integer qty;
private String imgPath;
private CategoryDto category;
}
My issue is concerning what i return from the REST API :
my product list returns nothing :
"orderProducts": [
{
"productKeyId": null,
"name": null,
"price": 0.0,
"qty": 0,
"imgPath": null,
"category": null
},...
]

How to find the specified field in spring-data-jpa

I want to find the specified field with the interface which extends JpaSpecificationExecutor.but i have not idea.
for example,i just need to find id,nickname in the user entity,what shall I do?
#Service
public class UserService {
public Page<User> findAll(User user, Pageable pageable) {
List<Predicate> predicates = new ArrayList<>();
if (user != null) {
//some condition
}
query.where(predicates.toArray(new Predicate[predicates.size()]));
return query.getRestriction();
}
}
#Repository
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
}
#Data
#Entity
#Table(name = "sys_user")
public class Note implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(nullable = false, unique = true)
private String email;
#Column(nullable = false)
private String password;
#Column(nullable = false)
private String nickname;
}
Here is answer on your question: https://www.baeldung.com/spring-data-jpa-projections .

JPA manyToMany bidirectional mapping tries to use an not exist table

Bidirectional ManyToMany mapping is using EMPLOYEE_PROJECT relation table instead of EMP_PROJ table described in mapping. EMPLOYEE_PROJECT does not exist on db. Eclipselink version is 2.7.3 and jpa version is 2.2
#Entity
public class Employee extends Base implements Serializable {
/** explicit set serialVersionUID */
private static final long serialVersionUID = 1L;
#Column(name = "FIRSTNAME")
private String firstname;
#ManyToMany
#JoinTable(name = "EMP_PROJ", joinColumns = #JoinColumn(name = "EMP_ID"), inverseJoinColumns = #JoinColumn(name = "PROJ_ID"))
private List<Project> projects;
public List<Project> getProjects() {
return projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
Project Class
#Entity
public class Project extends Base implements Serializable {
/** explicit set serialVersionUID */
private static final long serialVersionUID = 1L;
#Column(name = "NAME")
private String name;
#ManyToMany(mappedBy="projects")
Set<Employee> employees = new HashSet<Employee>();
Problem is when query run
#Test
public void save_employee(){
EntityManager em = emf.createEntityManager();
Employee emp1 = em.find(Employee.class, new Integer(1));
Project p = new Project();
p.setName("Name1");
p.getEmployees().add(emp1);
p = em.merge(p);
assertNotNull(p);
Employee emp2 = em.find(Employee.class, new Integer(1));
assertEquals(1, emp2.getProjects().size());
em.close();
}
Following exception:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLException: Table "EMPLOYEE_PROJECT" not found; SQL statement:
SELECT t1.ID, t1.DESCRIPTION, t1.NAME, t1.VERSION FROM EMPLOYEE_PROJECT t0, PROJECT t1 WHERE ((t0.Employee_ID = ?) AND (t1.ID = t0.projects_ID)) [42102-197]
Error Code: 42102
Base.java code is as following
#MappedSuperclass
public class Base {
protected int id;
protected int version;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}

Jpa Auditing dont save data in table auditing

I have to implementes Auditing in my aplication.. i inserting this data correctly
but i want to save all atributter from my Entity ,
Exemple, name, epigrafe, .. and olthers.
I implemented the mothod but dosent work, just dont save the atributte..
lets see..
#Entity
#EntityListeners(AuditingEntityListener.class)
#Table(name = "logradouros_historico", schema = "aud")
public class LogradourosHistorico {
#Id
#GeneratedValue
private Long id;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "id_logradouro")
private Logradouros logradouro;
#CreatedBy
private String modificadoPor;
#CreatedDate
#Temporal(TemporalType.TIMESTAMP)
private Date modifiedDate = new Date();
#Enumerated(EnumType.STRING)
private Acoes acao;
#Column(name = "nome")
private String nome; //nome do logradouro
public LogradourosHistorico() {
super();
}
public LogradourosHistorico(Logradouros logradouro, String modificadoPor,
Acoes acao) {
super();
this.logradouro = logradouro;
this.modificadoPor = modificadoPor;
this.acao = acao;
}
//getters and setters
my class entityListner
public class LogradourosEntityListener {
#PostPersist
public void prePersist(Logradouros target) {
perform(target, Acoes.INSERTED);
}
#PreUpdate
public void preUpdate(Logradouros target) {
perform(target, Acoes.UPDATED);
}
#PreRemove
public void preRemove(Logradouros target) {
perform(target, Acoes.DELETED);
}
#Transactional()
private void perform(Logradouros target, Acoes acao) {
target.getNome();
EntityManager entityManager = BeanUtil.getBean(EntityManager.class);
entityManager.persist(new LogradourosHistorico(target, acao));
}
}
my class Logradouros
#Entity
#EntityListeners(LogradourosEntityListener.class)
#Table(name = "logradouros", schema = "glb", uniqueConstraints= #UniqueConstraint(columnNames={"id_entidade", "idLogradouro"}))
public class Logradouros extends Auditable<String> implements Serializable {
private static final long serialVersionUID = 3703309412387185484L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idLogradouro;
#Column(name = "cep_geral")
private String cepGeral;
#Column(name = "epigrafe")
private String epigrafe;
#NotNull
#Column(name = "nome")
private String nome;
#Column(name = "nome_exibicao")
private String nomeExibicao;
#JoinColumn(name = "id_entidade")
#ManyToOne(/*cascade = CascadeType.ALL*/)
private Entidades entidade;
#NotNull
#JoinColumn(name = "id_municipio")
#ManyToOne(/*cascade = CascadeType.ALL*/)
private Municipios municipio;
// gettrs and settrs
so what i did wrong because i cant get the nome of entity Logradouros