0

I want to create two tables using Hibernate with relation one to many in Postgres:

@Entity
@Table(name = "onboarding_tasks")
public class OnboardingTasks implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private Long id;

    @Column(name = "title", length = 100)
    private String title;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "onboarding_tasks")
    private List<OnboardingTaskItem> onboardingTasks = new ArrayList<>();

    @Column(name = "created_at")
    private LocalDateTime createdAt; 

}

@Entity
@Table(name = "onboarding_task_item")
public class OnboardingTaskItem implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name="task_id")
    private OnboardingTasks onboardingTasks;

    @Column(name = "description", length = 100)
    private String description;

    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
}

I have one onboarding_task with many onboarding_task_item. So I would like to be able to have one to many table relation using foreign key.

I use this SQL script to generate test data:

    INSERT into onboarding_tasks (business_name, created_at, meta_title, status, title, task_type, updated_at)
    SELECT
            'Business name ' || id AS business_name,
            (NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS created_at,
        left (md5(random()::text), 10) AS meta_title,
        left (md5(random()::text), 10) AS status,
        left (md5(random()::text), 10) AS title,
        left (md5(random()::text), 10) AS task_type,
        (NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS updated_at
    FROM generate_series(1,355) as g(id)

but I get this error during deployment with liquibase:

Caused by: liquibase.exception.DatabaseException: ERROR: relation "onboarding_tasks" does not exist
  Position: 13 [Failed SQL: (0) INSERT into onboarding_tasks (business_name, created_at, meta_title, status, title, task_type, updated_at)
SELECT
        'Business name ' || id AS business_name,
        (NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS created_at,
    left (md5(random()::text), 10) AS meta_title,
    left (md5(random()::text), 10) AS status,
    left (md5(random()::text), 10) AS title,
    left (md5(random()::text), 10) AS task_type,
    (NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS updated_at
FROM generate_series(1,355) as g(id)]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:430)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:87)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:159)
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1276)
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1258)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:622)
    ... 27 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: relation "onboarding_tasks" does not exist
  Position: 13
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322)
    at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308)
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:279)
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:426)

Do I need to use "@Eager" annotation or the issue is wrong SQL script. I can't find the issue causing this exception.

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • You need a liquibase changelog that creates the tables according to what Hibernate expects as you can see when running with hbm2ddl.auto = create – Christian Beikov Sep 13 '21 at 13:46

1 Answers1

0

first I think your class OnboardingTasks should be more like this:

@OneToMany(mappedBy = "onboardingTasks", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<OnboardingTaskItem> onboardingTasks = new ArrayList<>();

and another thing you should check is to add a migration folder contain the creation sql script for the two tables.