我有一个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 .service() for servlet [dispatcherServlet] in context with path[]抛出异常[请求处理失败;嵌套异常是javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2002]的根本原因。
你能帮我吗?我为我的英语感到抱歉,谢谢你。
ProductWarehouse
已经有Warehouse
在它,我不明白为什么你再次设置它通过从DB内的for循环取回。
我不认为这个方法中有任何for循环的必要性,而且正如你上面所描述的,你没有在任何地方定义多对多关系。在构建关系时,可以使用连接表,如下所示
如果你需要更多的信息,请分享更多的细节,你的需求和你所面临的错误。