-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: findbycriteria cant resolve property of Object inside Object
PostPosted: Thu May 07, 2009 9:23 am 
Newbie

Joined: Fri Mar 27, 2009 7:09 am
Posts: 3
Hi,
Im trying to fix one bug for couple days and I cant find in Internet any solution. So I decided to write here.
I will try to explain the issue:

So I have Class which is called Equipment:
Code:
public class Equipment{
    private Integer _id;
    private String _serialNo;
    private Employee _owner;
    private Status _status;
    private Equipment _extension;
(..)
   @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name = "status")
    public Status getStatus() {
        return _status;
    }

    public void setStatus(Status status) {
        _status = status;
    }

}


and the Class which is called Status
Code:
@Entity
@Table(name = "status")
public class Status {
   
    private Integer _id;
    private String _statusName;
   
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    public Integer getId() {
        return _id;
    }
    public void setId(Integer id) {
        _id = id;
    }
   
    @Basic
    @Column(name = "status_name")
    public String getStatusName() {
        return _statusName;
    }
    public void setStatusName(String statusName) {
        _statusName = statusName;
    }
}


Im trying to find every Equipment which statusName is "IN_STORE_HOUSE" so I wrote method in DAO:
Code:
    @SuppressWarnings("unchecked")
    public List<Equipment> findAllEquipmentsInStoreHouseByEmployee(
       Employee employee) {
           DetachedCriteria criteria = DetachedCriteria.forClass(Equipment.class);
           criteria.setFetchMode("status", FetchMode.JOIN);
            criteria.add(Property.forName("owner").eq(employee));
           criteria.add(Property.forName("status.statusName").eq("IN_STORE_HOUSE"));
           criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
               return getHibernateTemplate().findByCriteria(criteria);
}


When I use this method I get an error:
Code:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: statusName of: com.nsn.equipment.model.Equipment; nested exception is org.hibernate.QueryException: could not resolve property: statusName of: com.nsn.equipment.model.Equipment
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:647)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:413)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:371)
   at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:956)
   at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:949)
   at com.nsn.equipment.DAO.EquipmentDAO.findAllEquipmentsInStoreHouseByEmployee(EquipmentDAO.java:217)


My question is how can I get inside object like Status?
I know that I can walk around this:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Status.class);
criteria.add(Property.forName("statusName").eq("IN_STORE_HOUSE"));
List<Status> statuses = getHibernateTemplate().findByCriteria(criteria);
and then
DetachedCriteria criteria = DetachedCriteria.forClass(Equipment.class);
(..)
criteria.add(Property.forName("status").eq(statuses.get(0)));


But its not my goal. And I have many issues because of this problem. And I need to make them by hand and filter lists in for loops... which i would like to avoid. I would be greatful for any help n tips.


Top
 Profile  
 
 Post subject: Re: findbycriteria cant resolve property of Object inside Object
PostPosted: Thu May 07, 2009 10:43 am 
Newbie

Joined: Wed Jan 28, 2009 9:57 am
Posts: 16
Location: Leinfelden, Germany
Instead of:
Quote:
criteria.add(Property.forName("status.statusName").eq("IN_STORE_HOUSE"));

Try using:
Code:
criteria.createCriteria("status").add(Restrictions.eq("statusName", "IN_STORE_HOUSE"));

This worked for me in the past, hope it works for you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.