0

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.

  • This looks like similar error you have https://stackoverflow.com/a/28193142/1460591 – Youans Feb 02 '21 at 10:59
  • I suspect `transactionDate` field being mapped to a `2002` value which is not a timestamp so hibernate throws you this mapping exception, fix here is to fix data in database making sure that column contains proper timestamp values and everything would work fine – Youans Feb 02 '21 at 11:02
  • Can you share your application.yml? – Ricardo Feb 02 '21 at 11:16

1 Answers1

0

ProductWarehouse is already having Warehouse in it, I don't understand why are you again setting it up by fetching it from DB inside the for loop.

I don't see any necessity of the for loop in that method, also as you described above you haven't defined many to many relationship anywhere. while building the relationship you can use the join table as explained here

if you need more information, please share more details of your need and errors you are facing.

krishna thota
  • 182
  • 1
  • 3
  • 8
  • I want to transfer all the products from warehouse which having fromId to new warehouse but the warehouse entity doesn't have any relations with product I change only the column warehouse in foreach to new warehouse – DevAbdurrahman Feb 02 '21 at 11:59
  • ProductWarehouse entity stored the ids and stock for example warehouse number 1 having 40 product number2 When i want to make some changes, i try to set it on productwarehouse table – DevAbdurrahman Feb 02 '21 at 12:03