0

I'm trying to use @SecondaryTable in a JPA entity and want to explicitly set the name of the foreign key constraint between the primary and secondary table.

  1. Here is a simplified version of my entity:
@Entity
@Table(
        name = DatabaseStructures.TABLE_ACCOUNT,
        uniqueConstraints = @UniqueConstraint(
                name = DatabaseStructures.USERNAME_UNIQUE,
                columnNames = DatabaseStructures.USERNAME_COLUMN))
@SecondaryTable(
        name = DatabaseStructures.TABLE_PERSONAL_DATA,
        pkJoinColumns = @PrimaryKeyJoinColumn(
                name = "account_id",
                referencedColumnName = "id",
                foreignKey = @ForeignKey(
                        name = "fk_name",
                        value = ConstraintMode.CONSTRAINT
                )
        ))
@NoArgsConstructor
public class Account extends AbstractEntity {

    @Column(name = DatabaseStructures.USERNAME_COLUMN, nullable = false,
            length = DatabaseConstraints.USERNAME_MAX_LENGTH)
    @Getter
    private String username;

    @Column(name = DatabaseStructures.IS_ACTIVE_COLUMN, nullable = false)
    @Getter @Setter
    private boolean isActive;

    @Column(name = DatabaseStructures.IS_VERIFIED_COLUMN, nullable = false)
    @Getter @Setter
    private boolean isVerified;

    @Column(table = DatabaseStructures.TABLE_PERSONAL_DATA,
            name = DatabaseStructures.USER_FIRST_NAME_COLUMN, nullable = false,
            length = DatabaseConstraints.USER_FIRST_NAME_MAX_LENGTH)
    @Getter @Setter
    private String firstName;

    @Column(table = DatabaseStructures.TABLE_PERSONAL_DATA,
            name = DatabaseStructures.USER_LAST_NAME_COLUMN, nullable = false,
            length = DatabaseConstraints.USER_LAST_NAME_MAX_LENGTH)
    @Getter @Setter
    private String lastName;

    @Column(name = DatabaseStructures.USER_PASSWORD_COLUMN, nullable = false,
            length = DatabaseConstraints.USER_PASSWORD_LENGTH)
    @Getter @Setter
    private String password;

    @Column(table = DatabaseStructures.TABLE_PERSONAL_DATA,
            name = DatabaseStructures.USER_EMAIL_COLUMN, nullable = false,
            length = DatabaseConstraints.USER_EMAIL_MAX_LENGTH)
    @Getter @Setter
    private String email;
}

  1. It's my DataSource config:
@Configuration
@Profile("dev")
@EnableJpaRepositories(entityManagerFactoryRef = "initEntityManagerFactory")
public class InitDataSourceConfig {

    @Value("${database.datasource.database.url}")
    private String databaseUrl;

    @Value("${database.datasource.init.username}")
    private String username;

    @Value("${database.datasource.init.password}")
    private String password;

    @Value("${database.datasource.init.maximum-pool-size}")
    private int maxPoolSize;

    @Value("${database.datasource.init.dll.auto}")
    private String dllAuto;

    @Value("${database.datasource.init.data-init-sql}")
    private String dataInitSql;

    @Primary
    @Bean(name = "initDataSource")
    public DataSource initDataSource() {
        HikariConfig dataSourceHikariConfig = new HikariConfig();
        dataSourceHikariConfig.setJdbcUrl(databaseUrl);
        dataSourceHikariConfig.setUsername(username);
        dataSourceHikariConfig.setPassword(password);
        dataSourceHikariConfig.setMaximumPoolSize(maxPoolSize);
        return new HikariDataSource(dataSourceHikariConfig);
    }

    @Primary
    @Bean(name = "initEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean initDataSourceEntityManagerFactory(
            EntityManagerFactoryBuilder builder, 
            @Qualifier("initDataSource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = builder
                .dataSource(dataSource)
                .packages("pl.myapp.entities")
                .persistenceUnit("initPU")
                .build();

        // setting Hibernate properties
        // -> show queries
        // -> generate schema (create-drop)
        // -> run init_data.sql
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(true);
        em.setJpaVendorAdapter(vendorAdapter);

        Properties hibernateAdditionalProperties = new Properties();
        hibernateAdditionalProperties.put("hibernate.hbm2ddl.auto", dllAuto);
        hibernateAdditionalProperties.put("hibernate.hbm2ddl.import_files", dataInitSql);
        em.setJpaProperties(hibernateAdditionalProperties);
        return em;
    }

    @Primary
    @Bean(name = "initTransactionManger")
    public PlatformTransactionManager initTransactionManager(
            @Qualifier("initEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

  1. When i run app it happpes:

Hibernate: alter table if exists personal_data drop constraint if exists FKltkulha4ixtuaa2fv19oxt9l1 <---- (shoud be fk_name :( ) Hibernate: drop table if exists accounts cascade Hibernate: drop table if exists personal_data cascade Hibernate: create table accounts (is_active boolean not null, is_verified boolean not null, version bigint not null, id uuid not null, password varchar(32) not null, username varchar(32) not null, primary key (id), constraint ACCOUNTS_USERNAME_UNIQUE unique (username)) Hibernate: create table personal_data (account_id uuid not null, email varchar(32) not null, first_name varchar(32) not null, last_name varchar(32) not null, primary key (account_id)) Hibernate: alter table if exists personal_data add constraint FKltkulha4ixtuaa2fv19oxt9l1 foreign key (account_id) references accounts`

Why does Hibernate ignore my custom foreign key name in @SecondaryTable?

I tried to change entity configuration:

@Entity
@Table(
        name = DatabaseStructures.TABLE_ACCOUNT,
        uniqueConstraints = @UniqueConstraint(
                name = DatabaseStructures.USERNAME_UNIQUE,
                columnNames = DatabaseStructures.USERNAME_COLUMN))
@SecondaryTable(
        name = DatabaseStructures.TABLE_PERSONAL_DATA,
        foreignKey = @ForeignKey(
                name = "my_fk_name",
                value = ConstraintMode.CONSTRAINT
        ),
        pkJoinColumns = @PrimaryKeyJoinColumn(
                name = "account_id",
                referencedColumnName = "id"
        ))
@NoArgsConstructor
public class Account extends AbstractEntity {

It didn't help.

Noobie

1
  • Looks like a bug. What version of Hibernate are you using, and have you looked at bugs on that version? Commented May 30 at 17:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.