Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的豆子里有一个非法的辩论例外_Java_Hibernate_Illegalargumentexception - Fatal编程技术网

Java 我的豆子里有一个非法的辩论例外

Java 我的豆子里有一个非法的辩论例外,java,hibernate,illegalargumentexception,Java,Hibernate,Illegalargumentexception,我刚开始使用hibernate,我的产品模型中出现了IllegalArgumentException。请帮帮我。。从昨晚开始我就一直在做这件事,直到现在我还没有找到解决办法。。如果你能帮我调试这个错误,我会很高兴的。谢谢 这是堆栈跟踪 Mar 15, 2014 6:26:55 PM org.hibernate.property.BasicPropertyAccessor$BasicSetter set ERROR: HHH000123: IllegalArgumentException

我刚开始使用hibernate,我的产品模型中出现了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具有Category实体中的categoryId和ProductTypes实体中的typeId

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;
    }


}
类别

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;
    }

}
试试这个:

private Category category;
然后呢,

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

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

然后使用
category.getId()访问id

嘿,我相信由于映射
@manytone(targetEntity=Category.class)
Hibernate试图将实际的POJO引用存储到您的
产品中。请将
类别ID
的类型更改为您的
类别
POJO好吗?可能与
typeId
相同,尝试将类型更改为您的
ProductType
POJO。这个问题显示了对ORM工作原理的一些基本误解,我将从阅读a开始。我已更新了问题,并包括了类别和产品类型。请帮帮我。谢谢我要试试这个solution@Erika或者只需从现有代码中删除
@ManyToOne
@JoinColumn
注释(在
getCategoryId()
方法上方),并仅使用
@Column(name=“category_id”)