14

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).

GreenieMeanie
  • 3,560
  • 4
  • 34
  • 39

3 Answers3

10

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.

Chris Wong
  • 101
  • 1
  • 3
6

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() {
    ..
  }

}
Trevor Allred
  • 888
  • 2
  • 13
  • 22
0

I'd double check your real implementation. I used your sample code and after adding an @Id everything worked as expected. Even IntelliJ says getBSet() is associated with B.owner.

danieljimenez
  • 1,390
  • 4
  • 17
  • 26
  • Did you actually run it and create the Session Factory? Thanks - I'll double check. I didn't use this exact same code - I just pulled from a real example I had that does the same type of thing... – GreenieMeanie Jun 08 '09 at 14:04
  • Yeah I did run it. That's why I mentioned the @ID. The SF complained about the @id being missing. After that the SF built okay. I could have sworn I typed that exact sentence, hah! Good luck! – danieljimenez Jun 08 '09 at 14:27