I have a 3 models and 1 table to Many to many relationship on my project this:
@Embeddable
@Getter
@Setter
public class ProductWarehouseId implements Serializable {
@Column(name = "warehouse_Id")
private Long warehouseId;
@Column(name = "product_Id")
private Long productId;
@Column(name = "user_Id")
private Long userId;
public ProductWarehouseId() {
}
public ProductWarehouseId(Long warehouseId, Long productId, Long userId) {
this.warehouseId = warehouseId;
this.productId = productId;
this.userId = userId;
}
}
---------------------------------------------------
@Entity
@NoArgsConstructor
@Getter
@Setter
public class ProductWarehouse {
@EmbeddedId
ProductWarehouseId productWarehouseId;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("productId")
@JoinColumn(name = "product_id")
ProductEntity product ;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("warehouseId")
@JoinColumn(name = "warehouse_id")
WarehouseEntity warehouse ;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("userId")
@JoinColumn(name = "user_id")
UserEntity userEntity;
@Column(name = "stockAmount")
private Long stockAmount;
@Column(name = "transctionDate")
@Temporal(TemporalType.TIMESTAMP)
private Date transactionDate = new Date();
public ProductWarehouse(ProductEntity product, UserEntity user) {
this.product = product;
this.userEntity = user;
}
}
********************************************************
@Getter
@Setter
@Entity
@RequiredArgsConstructor
public class ProductEntity extends BaseEntity{
@OneToMany(mappedBy = "product",cascade = CascadeType.ALL)
private Set<ProductWarehouse> productWarehouses;
//And more veriables
}
------------------------------------
@Getter
@Setter
@Entity
public class WarehouseEntity extends BaseEntity{
@OneToMany(mappedBy = "warehouse",cascade = CascadeType.ALL)
private Set<ProductWarehouse> productWarehouses = new HashSet<>();
//and more veriables
}
When i trying to select list from product_warehouse table to make changes, i have some Exceptions. I want to transfer the products between warehouses using fromId and toId
I using this method in service class:
@Override
@Transactional
public void transfer(Long fromId, Long toId) {
WarehouseEntity warehouseEntity = warehouseCRUDRepository.getOne(fromId);
WarehouseEntity warehouseEntity1 = warehouseCRUDRepository.getOne(toId);
if (warehouseEntity.getStatus().equals(WarehouseStatus.ACTIVE) && warehouseEntity1.getStatus().equals(WarehouseStatus.ACTIVE)){
Collection<ProductWarehouse> productWarehouses = em
.createNativeQuery("select c from product_warehouse c where c.warehouse_id =:fromId")
.setParameter("fromId",fromId)
.getResultList();
for (ProductWarehouse p : productWarehouses){
p.getProductWarehouseId().setWarehouseId(toId);
p.setWarehouse(warehouseCRUDRepository.getOne(toId));
}
}
}
And the Exception is :
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2002] with root cause.
Can you hep me please. I am sorry for my English, and thank you.