1

Has received the first test task in my life for a job, in which I need to do a restful service. One of the requirements is: "The application should be built using maven without installing or configuring any additional components;" I wrote my enterprise application before this, but I created the database itself manually through pgAdmin, and with the source code I ran a sql script that created tables like:

DROP TABLE IF EXISTS buildings CASCADE;

Now, as I understand it, my application starts, my code has to dynamically create the database somehow, or something else may be required, such as a remote database. I'm not sure, as, again, I've never done a test task before. Please tell me who is more experienced to the younger colleague)

Ilya
  • 135
  • 1
  • 2
  • 10

2 Answers2

1

CREATE DATABASE

If you want to do this from Java, use JDBC to connect to an existing database on the Postgres server. Every Postgres cluster has a default database automatically created, so that solves the chicken-or-egg dilemma.

In your JDBC’s SQL statement, use a login with sufficient privileges to create a database. Then write the text for a SQL command that uses CREATE DATABASE command.

You should also pay attention to creating other user accounts on the Postgres server, users with more restrictions than the superuser. Assign roles accordingly to your newly created database.

You can see this CREATE DATABASE idea in action on this Question, Postgresql - Create Database & Table dynamically. While framed in C# rather than Java, the concepts are the same.

Schema migration tools

Also, I suggest you consider adopting the use of a schema migration tool such as Flyway or Liquibase. But as I recall with both tools the database must already be created first. Then these tools can do the rest, creating tables and columns, indexes, constraints, and so on.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Hibernate expects the database to be present during runtime as it checks in your application.properties / application.yml file.

  1. If the database needs to be created by your application, you may want to check Flyway.

  2. If the database already exists but your application needs to handle the generation of tables, then you should model your entity since Hibernate will create this for you. See example below.

 #application.yml
 datasource:
   url: jdbc:postgresql://localhost:5432/mydb
     username: test
     password: test
   jpa:
     hibernate:
       ddl-auto: create-drop
     properties:
       hibernate:
         dialect: org.hibernate.dialect.PostgreSQLDialect
         format_sql: true
     show-sql: true

Then in your entity:

@Entity(name = "Employee")
@Table(name = "employee")
public class Employee {

  @Id
  @Column(name = "id", updatable = false, nullable = false)
  private Long id;

  @Column(name = "first_name", nullable = false, columnDefinition = "TEXT")
  private String firstName;
}

When you run your application, it should generate the database table named employee. Be aware of the ddl-auto value.

References:

Rye
  • 445
  • 4
  • 15