尝试添加具有ManyToOne关系的新实体,并使用tomcat7服务器启动提供java.lang.reflect.InvocationTargetException

时间:2015-01-19 07:06:17

标签: java spring hibernate jpa tomcat7

新增实体

    import java.util.Date;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.ForeignKey;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    import org.hibernate.annotations.Index;

    @Entity
    @Table(name = "ie_favorite")
    public class Favorite {

        @ManyToOne(targetEntity = Customer.class, optional = false)
        @JoinColumn(name = "customer_id", nullable = false, foreignKey =      @ForeignKey(name = "favorite_customerid"))
        @Index(name = "favorite_customer_index", columnNames = { "customer_id" })
        protected Customer customer;


        @ManyToOne(targetEntity = Product.class)
        @JoinColumn(name = "product_id", foreignKey = @ForeignKey(name = "favorite_productid"))
        @Index(name = "favorite_product_index", columnNames = { "product_id" })
        protected Product product;

        @Column(name = "created_date")
        @Temporal(TemporalType.TIMESTAMP)
        protected Date createdDate;

        public Customer getCustomer() {
            return customer;
        }

        public void setCustomer(Customer customer) {
            this.customer = customer;
        }

        public Product getProduct() {
            return product;
        }

        public void setProduct(Product product) {
            this.product = product;
        }

        public Date getCreatedDate() {
            return createdDate;
        }

        public void setCreatedDate(Date createdDate) {
            this.createdDate = createdDate;
        }

        @Override
        public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((createdDate == null) ? 0 : createdDate.hashCode());
        result = prime * result + ((customer == null) ? 0 : customer.hashCode());
        result = prime * result + ((product == null) ? 0 : product.hashCode());
        return result;
        }

        @Override
        public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Favorite other = (Favorite) obj;
        if (createdDate == null) {
            if (other.createdDate != null)
            return false;
        } else if (!createdDate.equals(other.createdDate))
            return false;
        if (customer == null) {
            if (other.customer != null)
            return false;
        } else if (!customer.equals(other.customer))
            return false;
        if (product == null) {
            if (other.product != null)
            return false;
        } else if (!product.equals(other.product))
            return false;
        return true;
        }

    }

使用以下代码更新客户实体

@OneToMany(mappedBy = "customer", targetEntity = Favorite.class, fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, orphanRemoval = true)
protected Set<Favorite> customerFavorites = new LinkedHashSet<Favorite>();

0 个答案:

没有答案