【问题标题】:I have Mapping Exception on my Spring Boot project我的 Spring Boot 项目有映射异常
【发布时间】:2021-05-06 13:11:17
【问题描述】:

我的项目中有 3 个模型和 1 个表到多对多关系 这个:

@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
}

当我尝试从 product_warehouse 表中选择列表进行更改时,我有一些例外。 我想使用 fromId 和 toId 在仓库之间转移产品

我在服务类中使用这个方法:

 @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));
            }
        }

    }

例外是:


servlet [dispatcherServlet] 在路径 [] 的上下文中的 Servlet.service() 引发异常 [请求处理失败;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2002] 有根本原因。


你能帮帮我吗? 对不起我的英语,谢谢。

【问题讨论】:

  • 这看起来像您遇到的类似错误stackoverflow.com/a/28193142/1460591
  • 我怀疑 transactionDate 字段被映射到不是时间戳的 2002 值,因此休眠会抛出此映射异常,此处修复是修复数据库中的数据,确保该列包含正确的时间戳价值观,一切都会好起来的
  • 你能分享你的application.yml吗?

标签: java spring spring-boot hibernate hibernate-mapping


【解决方案1】:

ProductWarehouse 里面已经有Warehouse,我不明白你为什么要通过在 for 循环中从 DB 中获取它来重新设置它。

我没有看到该方法中的 for 循环有任何必要性,而且正如您在上面描述的那样,您没有在任何地方定义多对多关系。在建立关系时,您可以按照here 的说明使用连接表

如果您需要更多信息,请分享更多关于您的需求和您面临的错误的详细信息。

【讨论】:

  • 我想将所有具有fromId的仓库的产品转移到新仓库但仓库实体与产品没有任何关系我只将foreach中的列仓库更改为新仓库
  • ProductWarehouse 实体存储了 ID 和库存,例如仓库编号 1 有 40 个产品编号2 当我想进行一些更改时,我尝试将其设置在 productwarehouse 表上
猜你喜欢
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 2016-09-13
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多