[hibernate-issues] [Hibernate-JIRA] Created: (HHH-4469) NullPointerException on ManyToMany association at line org.hibernate.type.AbstractType.getHashCode(AbstractType.java:136)

Sachin Mishra (JIRA) noreply at atlassian.com
Wed Sep 30 23:48:50 EDT 2009


NullPointerException on ManyToMany association  at line org.hibernate.type.AbstractType.getHashCode(AbstractType.java:136)
--------------------------------------------------------------------------------------------------------------------------

                 Key: HHH-4469
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4469
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.3.2
         Environment: 2009-10-01 13:27:28,062 INFO [org.hibernate.cfg.annotations.Version] - Hibernate Annotations 3.4.0.GA
2009-10-01 13:27:28,078 INFO [org.hibernate.cfg.Environment] - Hibernate 3.3.2.GA
2009-10-01 13:27:28,078 INFO [org.hibernate.cfg.Environment] - hibernate.properties not found
2009-10-01 13:27:28,078 INFO [org.hibernate.cfg.Environment] - Bytecode provider name : javassist
2009-10-01 13:27:28,078 INFO [org.hibernate.cfg.Environment] - using JDK 1.4 java.sql.Timestamp handling
2009-10-01 13:27:28,141 INFO [org.hibernate.annotations.common.Version] - Hibernate Commons Annotations 3.1.0.GA
2009-10-01 13:27:28,141 INFO [org.hibernate.ejb.Version] - Hibernate EntityManager 3.3.1.GA

            Reporter: Sachin Mishra
         Attachments: hibernate-log.txt

NullPointer Exception gets thrown on manytomany association when using composite key. Following the example from site http://boris.kirzner.info/blog/archives/2008/07/19/hibernate-annotations-the-many-to-many-association-with-composite-key/

Loading the product entity correctly loads everything as expected but saving new product record doesn't work when using the composite keys.

Item.java
@Entity
@Table(name="item")
public class Item implements Serializable {
    private Integer id;
    private String name;
    private List<ProductItem> productItems = new LinkedList<ProductItem>();
    private static Logger logger = Logger.getLogger(Item.class);


    /**
     * @return the id
     */
      @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="item_id")
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the productItems
     */
    @OneToMany(fetch=FetchType.LAZY,mappedBy="pk.item",cascade={CascadeType.ALL})
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
 
    public List<ProductItem> getProductItems() {
        return productItems;
    }

    /**
     * @param productItems the productItems to set
     */
    public void setProductItems(List<ProductItem> productItems) {
        this.productItems = productItems;
    }

    @Override
    public int hashCode() {
        int result;
        result = (this.id != null ? id.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Item other = (Item) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

}

Product.java
@Entity
@Table(name="product")
public class Product implements Serializable {
   private static Logger logger = Logger.getLogger(Product.class);

    private Integer id;
   
    private String name;
   
    private String description;
  
    private double regularPrice;
   
    private List<ProductItem> productItems = new LinkedList<ProductItem>();


    /**
     * @return the id
     */
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="prod_id")
    public Integer getId() {
        return id;
    }

    /**
     * @return the name
     */
     @Column(name="prod_name")
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

      
    /**
     * @return the productItems
     */
     @OneToMany(fetch=FetchType.LAZY,mappedBy="pk.product",cascade={CascadeType.ALL})
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public List<ProductItem> getProductItems() {
        return productItems;
    }

    /**
     * @param productItems the productItems to set
     */
    public void setProductItems(List<ProductItem> productItems) {
        this.productItems = productItems;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

ProductItem.java
@Entity
@Table(name = "productItem")
@AssociationOverrides({
@AssociationOverride(name = "pk.product", joinColumns = @JoinColumn(name = "product_id")),
@AssociationOverride(name = "pk.item", joinColumns = @JoinColumn(name = "item_id"))
        })

public class ProductItem implements Serializable{
      private static Logger logger = Logger.getLogger(ProductItem.class);

        private ProductItemPk pk ;
   

    /**
     * @return the item
     */
    @Transient
    public Item getItem() {
        return getPk().getItem();
    }

    /**
     * @param item the item to set
     */

    public void setItem(Item item) {
       getPk().setItem(item);
    }

    /**
     * @return the Product
     */
    @Transient
    public Product getProduct() {
        return getPk().getProduct();
    }

    /**
     * @param Product the Product to set
     */
    public void setProduct(Product Product) {
        getPk().setProduct(Product);
    }

    /**
     * @return the pk
     */
     @EmbeddedId
    public ProductItemPk getPk() {
        return pk;
    }

    /**
     * @param pk the pk to set
     */
    public void setPk(ProductItemPk pk) {
        this.pk = pk;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ProductItem that = (ProductItem) o;

        if (getPk() != null ? !getPk().equals(that.getPk()) : that.getPk() != null) return false;

        return true;
    }
    @Override
    public int hashCode() {
        return (getPk() != null ? getPk().hashCode() : 0);
    }
ProductItemPk.java
@Embeddable
public class ProductItemPk implements Serializable{
    private static Logger logger = Logger.getLogger(ProductItemPk.class);
    private Item item;
  
    private Product product;

    /**
     * @return the item
     */
     @ManyToOne(optional=false)
    public Item getItem() {
        return item;
    }

    /**
     * @param item the item to set
     */
    public void setItem(Item item) {
        this.item = item;
    }

    /**
     * @return the product
     */
      @ManyToOne(optional=false)
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ProductItemPk that = (ProductItemPk) o;

        if (item != null ? !item.equals(that.item) : that.item != null) return false;
        if (product != null ? !product.equals(that.product) : that.product != null)
            return false;

        return true;
    }
    @Override
    public int hashCode() {
        logger.error("productitemPK hashcode called");
        int result;
        result = (item != null ? item.hashCode() : 0);
        result = 31 * result + (product != null ? product.hashCode() : 0);
        return result;
    }


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list