Hibernate: как расширить BaseEntity и собственный BaseKey primaryKey? - PullRequest
0 голосов
/ 11 апреля 2020

У меня есть проект Spring с hibernate-core-4.3.0.Final со следующими объектами:

@Getter
@Setter
@Embeddable
@MappedSuperclass
public class BaseKey {
    @Column(name = "FIRST_COLUMN", nullable = false, length = 10)
    private String firstColumn;
    @Column(name = "SECOND_COLUMN", nullable = false, length = 10)
    private String secondColumn;
}

@Getter
@Setter
@Entity
public class BaseEntity implements Serializable {
    @EmbeddedId
    private BaseKey primaryKey;

    private String aField;
    private String anotherField;
}

Мне нужно расширить BaseEntity для добавления настроек в реализацию, которую я делаю. Мне также нужно использовать другой ключ, потому что мне нужен третий столбец в идентификаторе.

Но я не могу напрямую изменить BaseKey.

Я попробовал следующее:

@Getter
@Setter
@Embeddable
public class ExtendedKey extends BaseKey {
    @Column(name = "THIRD_COLUMN", nullable = false, length = 10)
    private String thirdColumn;
}

@Getter
@Setter
@Entity
public class ExtendedEntity extends BaseEntity implements Serializable {
    @EmbeddedId
    private ExtendedKey primaryKey;

    private String extraField;
}

Но у меня есть следующее исключение:

org.hibernate.AnnotationException: ExtendedKey must not have @Id properties when used as an @EmbeddedId: ExtendedEntity.primaryKey
    at org.hibernate.cfg.AnnotationBinder.bindComponent(AnnotationBinder.java:2353)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2069)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:834)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:753)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3762)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3716)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
    ... 64 more

Что мне нужно сделать, чтобы достичь цели? Возможно ли с этой версией Hibernate?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...