Elhamo
Elhamo

Reputation: 11

Hibernate twice embedded entities

I have Hibernate 5.2.10 version and hibernate-jpa-2.1-api with version 1.0.0.Final. I am using MairaDB as database. In persistance.xml, set the property hibernate.ejb.naming_strategy as DefaultComponentSafeNamingStrategy but still I receive the same error: Repeated column in mapping for entity. I do not want to use @attributeoverrides hibernate and I tried different methodes but still the same error. I want two or more embedded enities.

Thanks

Upvotes: 1

Views: 901

Answers (1)

v.ladynev
v.ladynev

Reputation: 19976

You can't use DefaultComponentSafeNamingStrategy with Hibernate 5, because of it is an implementation of the old NamingStrategy interface from Hibernate 4.

As you probably know, Hibernate 5 uses two new interfaces ImplicitNamingStrategy and PhysicalNamingStrategy.

You can use this implicit naming strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl. You will need to set hibernate.implicit_naming_strategy property (not hibernate.ejb.naming_strategy).

For these entities

@Embeddable
public class AuthorInfo {

    @Column
    private String authorInfo;

    @OneToOne
    private Book bestBook;

}

@Entity
public class Book {

    @Id
    private Long pid;

    @Embedded
    private AuthorInfo firstAuthor;

    @Embedded
    private AuthorInfo secondAuthor;

}

it creates this schema

create table Book (
        pid bigint not null,
        firstAuthor_authorInfo varchar(255),
        secondAuthor_authorInfo varchar(255),
        firstAuthor_bestBook_pid bigint,
        secondAuthor_bestBook_pid bigint,
        primary key (pid)
)

Unit test to check a schema: TwoEmbeddedStrategyTest.java

Upvotes: 1

Related Questions