开发者

Hibernate criteria upon associated objects

开发者 https://www.devze.com 2023-04-06 06:27 出处:网络
There is a class\"Item\" andit has some associated classes called Vehicle,Vmodel,Category,ItemName,Brand,SizeModel.Those each class has properites id and a name(for example Vehicle class, \"vid\" and

There is a class"Item" and it has some associated classes called Vehicle,Vmodel,Category,ItemName,Brand,SizeModel.Those each class has properites id and a name(for example Vehicle class, "vid" and "vname").Item class has itemcode.

Also I need to get Item objects from a given sample Item object(called "sItem") which equal to my sample item object's properties and my object's associated objects properties. Here is my code

Session session = getSession();
        List list = null;
        try {
            list = session.createCriteria(Item.class).add(Example.create(sItem))
                    .createCriteria("vehicle").add(Example.create(sItem.getVehicle())).开发者_Python百科
                    createCriteria("vmodel").add(Example.create(sItem.getVmodel())).
                    createCriteria("category").add(Example.create(sItem.getCategory())).
                    createCriteria("itemName").add(Example.create(sItem.getItemName())).
                    createCriteria("brands").add(Example.create(sItem.getBrands())).
                    createCriteria("sizeModel").add(Example.create(sItem.getSizeModel())).
                    list();
        } catch (HibernateException e) {
            e.printStackTrace();
        }

I refered this (15.6 section and last sample code in that section).

when this above code is executed, an error occures (org.hibernate.QueryException: could not resolve property: vmodel of: Entity.Vehicle). Please any one let me know where is the problem and where should I checked. My all mappings and other configuring classes are created with Netbeans IDE.


Try to split your criteria creation code like this:

Criteria baseCrit = session.createCriteria(Item.class).add(Example.create(sItem));
baseCrit.createCriteria("vehicle").add(Example.create(sItem.getVehicle()));
baseCrit.createCriteria("vmodel").add(Example.create(sItem.getVmodel()));
baseCrit.createCriteria("category").add(Example.create(sItem.getCategory()));
baseCrit.createCriteria("itemName").add(Example.create(sItem.getItemName()));
baseCrit.createCriteria("brands").add(Example.create(sItem.getBrands()));
baseCrit.createCriteria("sizeModel").add(Example.create(sItem.getSizeModel()));
list = baseCrit.list();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号