Я имею IllegalArgumentException в моем бобе

1

Я новичок в спящем режиме, и у меня есть исключение IllegalArgumentException в моей модели продукта. Пожалуйста, помогите мне.. Я работаю над этим с прошлой ночи, и до сих пор я не могу найти решение на этом. Будет приятно, если вы поможете мне отладить эту ошибку. благодаря

Это трассировка стека

Mar 15, 2014 6:26:55 PM org.hibernate.property.BasicPropertyAccessor$BasicSetter set
    ERROR: HHH000123: IllegalArgumentException in class: com.nutsaboutcandywebproject.model.Product, setter method of property: categoryId
    Mar 15, 2014 6:26:55 PM org.hibernate.property.BasicPropertyAccessor$BasicSetter set
    ERROR: HHH000091: Expected type: java.lang.Integer, actual value: com.nutsaboutcandywebproject.model.Category
    Mar 15, 2014 6:26:55 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [MainPageServlet] in context with path [/NutsAboutCandyWebProject] threw exception
    IllegalArgumentException occurred while calling setter for property [com.nutsaboutcandywebproject.model.Product.categoryId (expected type = java.lang.Integer)]; target = [com.nutsaboutcandywebproject.model.Product@1d60c02], property value = [com.nutsaboutcandywebproject.model.Category@11cf257]
        at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:123)
        at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:713)
        at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:362)
        at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:4718)
        at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:188)
        at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
        at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
        at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
        at org.hibernate.loader.Loader.doQuery(Loader.java:920)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
        at org.hibernate.loader.Loader.doList(Loader.java:2553)
        at org.hibernate.loader.Loader.doList(Loader.java:2539)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
        at org.hibernate.loader.Loader.list(Loader.java:2364)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
        at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
        at com.nutsaboutcandywebproject.dao.SQLProductsDataAccess.getInventory(SQLProductsDataAccess.java:31)
        at com.nutsaboutcandywebproject.service.ServiceFacadeImpl.getAllProducts(ServiceFacadeImpl.java:124)
        at com.nutsaboutcandywebproject.servlets.MainPageServlet.doGet(MainPageServlet.java:35)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:724)
    Caused by: java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:68)
        ... 39 more

Мой bean is here. Продукт bean имеет categoryId из объекта категории и typeId из ProductTypes entity

package com.nutsaboutcandywebproject.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "products", catalog = "db_nutsaboutcandy")
public class Product {

    private Integer id;
    private Integer categoryId;
    private Integer typeId;
    private String name;
    private Integer quantityInStock;
    private Integer costPerGram;
    private String description;

    //default constructor
    public Product() {

    }

    public Product(Integer id, Integer categoryId, Integer typeId, String name,
            Integer costPerGram, Integer quantityInStock, String description) {
        super();
        setId(id);
        this.categoryId = categoryId;
        this.typeId = typeId;
        this.name = name;
        this.quantityInStock = quantityInStock;
        this.costPerGram = costPerGram;
        this.description = description;
    }

    public Product(Integer categoryId, Integer typeId, String name,
            Integer quantityInStock, Integer costPerGram, String description) {
        super();
        this.categoryId = categoryId;
        this.typeId = typeId;
        this.name = name;
        this.quantityInStock = quantityInStock;
        this.costPerGram = costPerGram;
        this.description = description;
    }



    @Id
    @GeneratedValue
    @Column(name = "product_id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }


    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(targetEntity = Category.class)
    @JoinColumn(name = "category_id", nullable = false)
    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    @ManyToOne(targetEntity = ProductTypes.class)
    @JoinColumn(name = "type_id", nullable = false)
    public Integer getTypeId() {
        return typeId;
    }


    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    @Column(name = "product_name", nullable = false)
    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "quantity_in_stock", nullable = false)
    public Integer getQuantityInStock() {
        return quantityInStock;
    }


    public void setQuantityInStock(Integer quantityInStock) {
        this.quantityInStock = quantityInStock;
    }

    @Column(name = "cost_per_gram", nullable = false)
    public Integer getCostPerGram() {
        return costPerGram;
    }


    public void setCostPerGram(Integer costPerGram) {
        this.costPerGram = costPerGram;
    }

    @Column(name = "description", nullable = false)
    public String getDescription() {
        return description;
    }


    public void setDescription(String description) {
        this.description = description;
    }


}

Category.class

package com.nutsaboutcandywebproject.model;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "product_categories", catalog = "db_nutsaboutcandy")
public class Category {

    private Integer categoryId;
    private String categoryName;
    private Integer shelfLifeDays;

    public Category(){

    }

    public Category(Integer categoryId, String categoryName,
            Integer shelfLifeDays) {
        super();
        this.categoryId = categoryId;
        this.categoryName = categoryName;
        this.shelfLifeDays = shelfLifeDays;
    }


    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "category_id", unique = true, nullable = false)
    public Integer getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    @Column(name = "category_name", nullable = false)
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    @Column(name = "shelf_life_days", nullable = false)
    public Integer getShelfLifeDays() {
        return shelfLifeDays;
    }
    public void setShelfLifeDays(Integer shelfLifeDays) {
        this.shelfLifeDays = shelfLifeDays;
    }

}

ProductTypes.class

package com.nutsaboutcandywebproject.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "product_types", catalog = "db_nutsaboutcandy")
public class ProductTypes {

    private Integer typeId;
    private String typeName;

    public ProductTypes(Integer typeId, String typeName) {
        super();
        this.typeId = typeId;
        this.typeName = typeName;
    }
    public ProductTypes(){

    }
    @Id
    @GeneratedValue
    @Column(name = "type_id", nullable = false)
    public Integer getTypeId() {
        return typeId;
    }
    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }

    @Column(name = "type_name", nullable = false)
    public String getTypeName() {
        return typeName;
    }
    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

}
Теги:
hibernate
illegalargumentexception

1 ответ

1
Лучший ответ

Попробуй это:

private Category category;

А потом,

@ManyToOne
@JoinColumn(name = "category_id", nullable = false)
public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

И затем войдите в id с помощью category.getId();

  • 0
    Я собираюсь попробовать это решение
  • 0
    @Erika Или просто удалите аннотации @ManyToOne и @JoinColumn из существующего кода (выше getCategoryId() ) и используйте только @Column(name = "category_id")
Показать ещё 1 комментарий

Ещё вопросы

Сообщество Overcoder
Наверх
Меню