Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Used mappedBy on class that extends another class annotated as JoinedSubclass?

The following doesn't work:

@Entity
class Owner {

  @OneToMany(mappedBy="owner", cascade = {CascadeType.ALL})
  protected Set<B> getBSet() {
    ..
  }

}

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
class A {
   @ManyToOne
   public Owner getOwner() {
     ...
   }
}

@Entity
class B extends A {

}

It causes an exception as such: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: B.user in Owner.

I am trying to avoid copying the "owner" property into class B (which will consequently "denormalize" and copy the owner key into both tables generated for entity A and B). Also, I would really like to have A and B in a separate table and not have to use a discriminator by using SingleTable inheritance.

Also, I can't figure out how to do something similar by using @OneToOne between A and B (and not having B extend A).

like image 234
GreenieMeanie Avatar asked Jun 07 '09 20:06

GreenieMeanie


People also ask

What is the use of mappedBy in Hibernate?

mappedBy attribute In JPA or Hibernate, entity associations are directional, either unidirectional or bidirectional. Always mappedBy attribute is used in bidirectional association to link with other side of entity. In the above tables, BRANCH and STUDENT tables has One-To-Many association.

What is the difference between @JoinColumn and mappedBy?

The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.

Which annotation is used for mapping is a relationship in Hibernate?

In hibernate we can map a model object into a relation/table with the help of @Entity annotation.

What does the attribute mappedBy defines?

mappedBy tells Hibernate how to create instances of your entities and load the data into them. It should refer to the field name in the class that you are annotating, PersonDetail in this instance, where the relationship is defined.


2 Answers

It's a Hibernate oddity, but it's deliberate. I have a blog post up with background information, links and a workaround for the JOINED solution.

like image 61
Chris Wong Avatar answered Oct 07 '22 16:10

Chris Wong


Try adding targetEntity = Transaction.class. This worked for me when I was using SINGLE_TABLE inheritance. I didn't try it with JOIN.

@Entity
class Owner {

  @OneToMany(mappedBy="owner", cascade = {CascadeType.ALL}, targetEntity = Transaction.class)
  @Where(clause = "tableType='I'")
  protected Set<B> getBSet() {
    ..
  }

}
like image 45
Trevor Allred Avatar answered Oct 07 '22 17:10

Trevor Allred