解决方案 »

  1.   

    java.util.HashSet cannot be cast to org.hibernate.collection.PersistentCollection 类型转换错误
    检查下代码,看是否有类型转换错误,或者映射中有类型映射的错误
      

  2.   


    我当然知道是转换错误,我是想知道这个是什么原因导致的转换错误?这个转成PersistentCollection是什么东西? 
      

  3.   

    Hibernate有自己的一套集合机制,不要直接转成HashSet,转成Set。
    以前也碰到过类似问题,但时间有点久远,脑袋又不好使,等大神来解决。
      

  4.   

    其实也就几句话
    goods关联了product  是一对多的关系
    我更改了goods里面的product的信息
    controller类代码
    this.goodsService.update(goods);serviceImpl类代码:
    Goods  good = (Goods)super.update(goods);
    update调用的是下面DaoImpl来实现的DaoImpl代码是调用的:
    this.session.merge(goods);merge的时候应该才报的这个问题
    实体类
    @Entity
    @Table(name="goods")
    public class Goods extends BaseEntity
    {
      private static final long serialVersionUID = -6977025562650112419L;
      private Set<Product> products = new HashSet<Product>();  @OneToMany(mappedBy="goods", fetch=FetchType.EAGER, cascade={javax.persistence.CascadeType.ALL}, orphanRemoval=true)
      public Set<Product> getProducts()
      {
        return this.products;
      }@Entity
    @Table(name="product")
    public class Product extends BaseEntity{
      
      private Goods good;
      private List<ProductImage> productImages = new ArrayList();
      private Set<Review> reviews = new HashSet();
      private Set<Consultation> consultations = new HashSet();
      private Set<Tag> tags = new HashSet();
      private Set<Member> favoriteMembers = new HashSet();
      private Set<Specification> specifications = new HashSet();
      private Set<SpecificationValue> specificationValues = new HashSet();
      private Set<Promotion> promotions = new HashSet();
      private Set<CartItem> cartItems = new HashSet();
      private Set<OrderItem> orderItems = new HashSet();
      private Set<GiftItem> giftItems = new HashSet();
      private Set<ProductNotify> productNotifies = new HashSet();
      private Map<MemberRank, BigDecimal> memberPrice = new HashMap();
      private Map<Parameter, String> parameterValue = new HashMap();
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(nullable=false, updatable=false)
      public Goods getGoods()
      {
        return this.good;
      }  public void setGoods(Goods goods)
      {
        this.good = goods;
      } 
      @Valid
      @ElementCollection
      @CollectionTable(name="product_product_image")
      public List<ProductImage> getProductImages()
      {
        return this.productImages;
      }  public void setProductImages(List<ProductImage> productImages)
      {
        this.productImages = productImages;
      }  @OneToMany(mappedBy="product", fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.REMOVE})
      public Set<Review> getReviews()
      {
        return this.reviews;
      }  public void setReviews(Set<Review> reviews)
      {
        this.reviews = reviews;
      }  @OneToMany(mappedBy="product", fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.REMOVE})
      public Set<Consultation> getConsultations()
      {
        return this.consultations;
      }  public void setConsultations(Set<Consultation> consultations)
      {
        this.consultations = consultations;
      }  @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="product_tag")
      @OrderBy("order asc")
      public Set<Tag> getTags()
      {
        return this.tags;
      }  public void setTags(Set<Tag> tags)
      {
        this.tags = tags;
      }  @ManyToMany(mappedBy="favoriteProducts", fetch=FetchType.LAZY)
      public Set<Member> getFavoriteMembers()
      {
        return this.favoriteMembers;
      }  public void setFavoriteMembers(Set<Member> favoriteMembers)
      {
        this.favoriteMembers = favoriteMembers;
      }  @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="product_specification")
      @OrderBy("order asc")
      public Set<Specification> getSpecifications()
      {
        return this.specifications;
      }  public void setSpecifications(Set<Specification> specifications)
      {
        this.specifications = specifications;
      }  @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="product_specification_value")
      @OrderBy("specification asc")
      public Set<SpecificationValue> getSpecificationValues()
      {
        return this.specificationValues;
      }  public void setSpecificationValues(Set<SpecificationValue> specificationValues)
      {
        this.specificationValues = specificationValues;
      }  @ManyToMany(mappedBy="products", fetch=FetchType.LAZY)
      public Set<Promotion> getPromotions()
      {
        return this.promotions;
      }  public void setPromotions(Set<Promotion> promotions)
      {
        this.promotions = promotions;
      }  @OneToMany(mappedBy="product", fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.REMOVE})
      public Set<CartItem> getCartItems()
      {
        return this.cartItems;
      }  public void setCartItems(Set<CartItem> cartItems)
      {
        this.cartItems = cartItems;
      }  @OneToMany(mappedBy="product", fetch=FetchType.LAZY)
      public Set<OrderItem> getOrderItems()
      {
        return this.orderItems;
      }  public void setOrderItems(Set<OrderItem> orderItems)
      {
        this.orderItems = orderItems;
      }  @OneToMany(mappedBy="gift", fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.ALL})
      public Set<GiftItem> getGiftItems()
      {
        return this.giftItems;
      }  public void setGiftItems(Set<GiftItem> giftItems)
      {
        this.giftItems = giftItems;
      }  @OneToMany(mappedBy="product", fetch=FetchType.LAZY, cascade={javax.persistence.CascadeType.REMOVE})
      public Set<ProductNotify> getProductNotifies()
      {
        return this.productNotifies;
      }  public void setProductNotifies(Set<ProductNotify> productNotifies)
      {
        this.productNotifies = productNotifies;
      }  @ElementCollection(fetch=FetchType.LAZY)
      @CollectionTable(name="product_member_price")
      public Map<MemberRank, BigDecimal> getMemberPrice()
      {
        return this.memberPrice;
      }  public void setMemberPrice(Map<MemberRank, BigDecimal> memberPrice)
      {
        this.memberPrice = memberPrice;
      }  @ElementCollection(fetch=FetchType.LAZY)
      @CollectionTable(name="product_parameter_value")
      public Map<Parameter, String> getParameterValue()
      {
        return this.parameterValue;
      }  public void setParameterValue(Map<Parameter, String> parameterValue)
      {
        this.parameterValue = parameterValue;
      }
      public void setProducts(Set<Product> products)
      {
        this.products = products;
      }
      

  5.   

    我看了我们这边代码逻辑,报这个转换异常肯定是做merge操作的时候发生的,那也就是做在操作实体的时候出现的这个问题,product类里面很多hashset的集合,因为我是修改了product,merge应该是做update操作,update的操作实体里面的集合会强制转成持久化的集合?