0

Entities:

@Entity
@Table(name = "ITEM")
@Inheritance(strategy = InheritanceType.JOINED)
public class Item extends Base {
    @OneToOne(mappedBy = "item")
    protected Doc doc;
}

@MappedSuperclass
public abstract class Doc extends BaseDoc {
@OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "itemId")
    private Item item;
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BaseDoc extends Base {}

Tables:

BASEDOC
- itemId int8
(other attributes)
ITEM
(other attributes)
BASE
(other attributes)

During runtime it fails with:

Caused by:

org.hibernate.AnnotationException: Unknown mappedBy in: com.ghiton.updater.entity.Item.doc, referenced property unknown: com.ghiton.updater.entity.Doc.item"}}

I think the reason is the MappedSuperclass, since 'item' is stored in the Base table. Is there a practice to solve these type of cases?

I found that "Mapped superclasses can't be targets of entity relationships.", in this case how I can achieve that Doc to be persisted into the BaseDoc table?

At DB level it has all the columns what are needed, so not necessary to have a separate DOC table.

ghithon
  • 1
  • 1

1 Answers1

0

You cant join mappedsuperclass annotated class with entity class. Mappedsuperclases are not an entity

Link

I think you can change your code like this.

@Entity
@Table(name = "ITEM")
@Inheritance(strategy = InheritanceType.JOINED)
public class Item extends Base {
    @OneToOne(mappedBy = "item")
    protected Doc doc;
}

@Entity
@Table(name = "DOC")
public abstract class Doc extends BaseDoc {
    @OneToOne(mappedBy = "doc")
    private Item item;
}

@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BaseDoc extends Base {}
  • At the moment I don't have DOC table, because I wanted to reuse BASEDOC so @Table(name = "DOC") is not good for Doc class. – ghithon Feb 02 '23 at 10:33