Alessandro
Alessandro

Reputation: 4472

org.hibernate.AnnotationException: Unknown mappedBy with common class

I've already seen many questions about the same argument, but I didn't find any solution. I've two classes that inherits the same class.

Basically:

@MappedSuperclass
public abstract class MyGeneric {
  private String idGeneric;
  public String getIdGeneric() {
    return idGeneric;
  }
  public void setIdGeneric(final String idGeneric) {
    this.idGeneric = idGeneric;
  }
}

@Entity
public class Child extends MyGeneric {
    // [some fields]
}


@Entity
public class Parent extends MyGeneric {
    @OneToOne(mappedBy = "idGeneric")
    private Child child;
}

But the application fails to run because:

org.hibernate.AnnotationException: Unknown mappedBy in: mypackage.Parent.child, referenced property unknown: mypackage.Child.idGeneric

I can't understand why it can find the property Child.idGeneric since it exists.

Thanks

Upvotes: 0

Views: 562

Answers (2)

Michal
Michal

Reputation: 1010

As you are not mapping the inverse side of the association, you cannot use mappedBy. Replace you mapping with the following:

@OneToOne 
@JoinColumn(name = "idGeneric")
private Child child;

Upvotes: 1

Ajit
Ajit

Reputation: 19

@Entity
public class Child extends MyGeneric {
    // [some fields]
    @OneToOne(mappedBy = "child")
    private Parent parent;
}

@Entity
public class Parent extends MyGeneric {
    @OneToOne(mappedBy = "parent")
    private Child child;
}

The above association should work.

Upvotes: 0

Related Questions