views:

1763

answers:

4

I have a Hibernate entity, with a getter that is mapped as a @OneToMany:

@Entity
class Parent extends BaseParent {

    @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "parent")
    public List<Child> getChildren() {
        return super.children;
    }

    public void setChildren(List<Child> list) {
        super.children = list;
    }
}

When I try to execute some HQL like:

select p 
from Parent p 
left join p.children c
where c.name='foobar'

I get the following exception:

org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: children of: ....Parent

If I put the @OneToMany annotation on the protected field, the query works. How can I get this to work such that the annotation can be placed on the getter?

A: 

I recommend reading the following section in the Hibernate documentation for Annotations:

http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e253

In this case, you can probably add an @AccessType("property") annotation at the class level, which tells Hibernate to scan the getters for this class.

If that doesn't work, read the documentation to see if there is something that is more tailored to your specific problem. It is hard giving you an exact answer w/o seeing the entire entity hierarchy.

mattsidesinger
+1  A: 

You may be having issues related to the mixing of fields and methods in your annotations. For example, if you have an @Id annotation in your parent class BaseParent, then Hibernate will by default look at fields and not methods.

So basically if you had something like this:

public class BaseParent {
    @Id
    private Integer id;

    protected List<Child> children;
}

public class Parent extends BaseParent {
    @ManyToOne
    public List<Child> getChildren() {super.getChildren();} 
}

Hibernate is going to have a problem determining the @ManyToOne annotation. Also, that would probably explain why adding the annotation to the field in the parent class made it work.

On the same page that is mentioned above (http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e253), there is also this paragraph:

Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. the getter method if you use property access, the field if you use field access. Mixing EJB3 annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

So, to get it to work, you probably want to choose one or the other (field vs method) and be consistent. That way, you will not come across these "strange" hibernate issues.

Anyways, hope that is helpful.

Nick Spilman
A: 

Yes, I needed the @AccessType("property") annotation. But the second answer was also correct: i am now having trouble with the association. This is great information, thanks for the help!

joekutner
A: 

When you have the "mappedBy" property in the @OneToMany annotation, it means your going to have a Bidirectional mapping. You need to have get/set or field for "parent" on the child. Example:

@Entity
public class Parent {
    @OneToMany(mappedBy="parent")
    public Set<Child> getChildren() {
    ...
}

@Entity
public class Child{
    @ManyToOne
    public Parent getParent() {
    ...
}

see: http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#d0e1177

Laplie