takotsubo
takotsubo

Reputation: 746

Mapping Java type to database type

I want to set type of my columns from my Java code manually (table is not created yet). String should be text type in database, and LocalDate should be date type. Afaik without mappings, String will be mapped as VARCHAR type.

Entity:

@Entity
@Table(schema = "subscribers_service", name = "subscribers")
public class Subscriber {

    @Id
    @Column(name = "email")
    private String email;

    @Column(name = "subscription_date")
    private LocalDate dateOfSubscription;

}

So I want kinda this:

    @Id
    @Column(name = "email")
    @DatabaseType(name = "text")
    private String email;

I'm using PotsgreSQL. Is it possible? Or I should manually create database with required column types?

Upvotes: 1

Views: 252

Answers (1)

S.Tushinov
S.Tushinov

Reputation: 648

You can set the database colum types via Java code using the annotation @Column

All you have to do is add columnDefinition to the annotation

Example

@Column(name = "email", columnDefinition = "text")

and

@Column(name = "subscription_date", columnDefinition = "date")

Hope this was helpful.

Upvotes: 1

Related Questions