Hibernate PropertyAccessException:发生IllegalArgumentException调用getter吗?发生了什么事?

时间:2019-02-19 17:45:12

标签: java hibernate orm illegalargumentexception

当我运行下面的代码以显示属于JTable中特定类别的项目时,我收到此异常: enter image description here

private void categoryComboBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
                try {
        DefaultTableModel dtm = new DefaultTableModel();
        String cat = this.categoryComboBox.getSelectedItem().toString();
        session=sessionFactory.openSession();
        transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(Items.class);
//HERE LIES THE PROBLEM BUT I HAVE NO CLUE HOW TO SOLVE IT
        List<Items> itemlist = criteria.add(Restrictions.eq("category", cat).ignoreCase()).list();
        transaction.commit();
        session.close();

            if (dtm.getColumnCount()==0) {
            dtm.addColumn("Name");
            dtm.addColumn("Category");
            dtm.addColumn("Quantity");
            dtm.addColumn("Net Price (per unit)");
            dtm.addColumn("Gross Pice (per unit)");
            dtm.addColumn("Net Price (total)");
            dtm.addColumn("Gross Price (total)");
            }
            Font f = new Font("Georgia", Font.BOLD, 16);      
            JTableHeader header = itemTable.getTableHeader();
            header.setFont(f);
            itemTable.setRowHeight(25);

            if (getQuantity().isEmpty()) {for (int i = 0; i < itemlist.size(); i++) {
                     dtm.addRow(new Object[]{itemlist.get(i).getName(), itemlist.get(i).getCategory().getName(),"-",itemlist.get(i).getNetPrice(), Math.round((Double.valueOf(itemlist.get(i).getNetPrice())*(1d+itemlist.get(i).getVatRate()/100d))), "-", "-"});
                }

            } else {
                for (int i = 0; i < itemlist.size(); i++) {
                    dtm.addRow(new Object[]{itemlist.get(i).getName(), itemlist.get(i).getCategory().getName(), getQuantity().get(itemlist.get(i).getItemId()).getQuantity(), itemlist.get(i).getNetPrice(), itemlist.get(i).getNetPrice()*(itemlist.get(i).getVatRate()+1), getQuantity().get(itemlist.get(i).getItemId()).getQuantity()*itemlist.get(i).getNetPrice(), itemlist.get(i).getNetPrice()*(itemlist.get(i).getVatRate()+1)*getQuantity().get(itemlist.get(i).getItemId()).getQuantity()});
                }

            }

          this.itemTable.setModel(dtm);} catch (Exception e) {
         JOptionPane.showMessageDialog(null, e.getMessage()); 
         e.printStackTrace();
    }
} 

这也是我的XML映射类:

Caregories.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 9, 2019, 3:57:52 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="classes.Categories" table="CATEGORIES" schema="APP" optimistic-lock="version">
        <id name="categoryId" type="int">
            <column name="CATEGORY_ID" />
            <generator class="native" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="100" />
        </property>
     <set name="items" table="ITEMS" inverse="false" cascade="all" lazy="false" fetch="select">
        <key>
            <column name="CATEGORY_ID" not-null="true" />
        </key>
        <one-to-many class="classes.Items" />
    </set>
    </class>
</hibernate-mapping>

Items.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="classes.Items" table="ITEMS" schema="APP" optimistic-lock="version">
        <id name="itemId" type="int" column="ITEM_ID">
            <generator class="native" />
        </id>
        <property name="name" type="string" column="NAME" length="100"/>
        <property name="netPrice" type="java.lang.Integer" column="NET_PRICE"/>
        <property name="vatRate" type="java.lang.Integer" column="VAT_RATE"/>
        <many-to-one name="category" class="classes.Categories" fetch="select" column="CATEGORY_ID" not-null="true" lazy="false"/>
    </class>
</hibernate-mapping>

这是我数据库的实体关系图 enter image description here

Categories.java

package classes;
// Generated Feb 9, 2019, 3:57:50 PM by Hibernate Tools 4.3.1

import java.util.Set;

public class Categories  implements java.io.Serializable {


     private int categoryId;
     private String name;
     private Set<Items> items;

    public Categories() {
    }

    public Categories(int categoryId, String name, Set<Items> items) {
        this.categoryId = categoryId;
        this.name = name;
        this.items = items;
    }  


    public int getCategoryId() {
        return this.categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }
    public String getName() {
        return this.name;
    }

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

    public Set<Items> getItems() {
        return items;
    }

    public void setItems(Set<Items> items) {
        this.items = items;
    }


}

Items.java

package classes;

public class Items  implements java.io.Serializable {


     private int itemId;
     private String name;
     private Integer netPrice;
     private Integer vatRate;
     private Categories category;

    public Items() {
    }

    public Items(int itemId, String name, Integer netPrice, Integer vatRate, Categories category) {
        this.itemId = itemId;
        this.name = name;
        this.netPrice = netPrice;
        this.vatRate = vatRate;
        this.category = category;
    }

    public int getItemId() {
        return this.itemId;
    }

    public void setItemId(int itemId) {
        this.itemId = itemId;
    }
    public String getName() {
        return this.name;
    }

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

    public Integer getNetPrice() {
        return this.netPrice;
    }

    public void setNetPrice(Integer netPrice) {
        this.netPrice = netPrice;
    }
    public Integer getVatRate() {
        return this.vatRate;
    }

    public void setVatRate(Integer vatRate) {
        this.vatRate = vatRate;
    }

    public Categories getCategory() {
        return category;
    }

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


}
  

org.hibernate.PropertyAccessException:IllegalArgumentException   发生在调用class.Categories.categoryId的getter   org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:65)     在   org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:224)     在   org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4933)     在   org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4633)     在   org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:226)     在   org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:276)     在org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)     在org.hibernate.type.EntityType.nullSafeSet(EntityType.java:288)处   org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:2102)     在org.hibernate.loader.Loader.bindParameterValues(Loader.java:2071)     在   org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2006)     在   org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1920)     在   org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1898)     在org.hibernate.loader.Loader.doQuery(Loader.java:937)处   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)     在org.hibernate.loader.Loader.doList(Loader.java:2695)处   org.hibernate.loader.Loader.doList(Loader.java:2678)在   org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2512)在   org.hibernate.loader.Loader.list(Loader.java:2507)在   org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)     在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1999)处   org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:370)位于   stock.view.StockView.categoryComboBoxActionPerformed(StockView.java:530)     在   stock.view.StockView $ FormListener.actionPerformed(StockView.java:344)     在   java.desktop / javax.swing.JComboBox.fireActionEvent(JComboBox.java:1264)     在   java.desktop / javax.swing.JComboBox.contentsChanged(JComboBox.java:1337)     在   java.desktop / javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:123)     在   java.desktop / javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:94)     在   java.desktop / javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:132)     在java.desktop / javax.swing.JComboBox.addItem(JComboBox.java:716)处   stock.view.StockView.FillUpComboBox(StockView.java:103)在   stock.view.StockView。(StockView.java:41)在   main.view.MainMenu.stockMenuItemActionPerformed(MainMenu.java:290)位于   main.view.MainMenu $ FormListener.actionPerformed(MainMenu.java:240)在   java.desktop / javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)     在   java.desktop / javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2308)     在   java.desktop / javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)     在   java.desktop / javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)     在   java.desktop / javax.swing.AbstractButton.doClick(AbstractButton.java:369)     在   java.desktop / javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1020)     在   java.desktop / javax.swing.plaf.basic.BasicMenuItemUI $ Handler.mouseReleased(BasicMenuItemUI.java:1064)     在   java.desktop / java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)     在   java.desktop / java.awt.Component.processMouseEvent(Component.java:6632)     在   java.desktop / javax.swing.JComponent.processMouseEvent(JComponent.java:3342)     在java.desktop / java.awt.Component.processEvent(Component.java:6397)     在java.desktop / java.awt.Container.processEvent(Container.java:2263)     在   java.desktop / java.awt.Component.dispatchEventImpl(Component.java:5008)     在   java.desktop / java.awt.Container.dispatchEventImpl(Container.java:2321)     在java.desktop / java.awt.Component.dispatchEvent(Component.java:4840)     在   java.desktop / java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)     在   java.desktop / java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)     在   java.desktop / java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)     在   java.desktop / java.awt.Container.dispatchEventImpl(Container.java:2307)     在java.desktop / java.awt.Window.dispatchEventImpl(Window.java:2772)     在java.desktop / java.awt.Component.dispatchEvent(Component.java:4840)     在   java.desktop / java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)     在java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:721)在   java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:715)在   java.base / java.security.AccessController.doPrivileged(本机方法)     在   java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)     在   java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)     在java.desktop / java.awt.EventQueue $ 5.run(EventQueue.java:745)在   java.desktop / java.awt.EventQueue $ 5.run(EventQueue.java:743)在   java.base / java.security.AccessController.doPrivileged(本机方法)     在   java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)     在   java.desktop / java.awt.EventQueue.dispatchEvent(EventQueue.java:742)     在   java.desktop / java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)     在   java.desktop / java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)     在   java.desktop / java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)     在   java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)     在   java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)     在   java.desktop / java.awt.EventDispatchThread.run(EventDispatchThread.java:90)   原因:java.lang.IllegalArgumentException:对象不是   在声明类的实例   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:566)在   org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:42)     ...还有71

1 个答案:

答案 0 :(得分:1)

criteria.add(Restrictions.eq("category", cat).ignoreCase()),这是错误的。您无法将关系与字符串进行比较。以下是直接来自Hibernate文档的示例:

List<Items> itemlist = sess.createCriteria(Items.class)      
    .createAlias("category",c)
        .add( Restrictions.eq("c.name", cat))
    .list();

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

另一个观察结果是您正在使用inverse = false。 IMO,您希望它是inverse = true