1

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?

1
  • Historically TEXT was for large strings, "memo" fields, not immediately stored with the record, not indexable, and VARCHAR for shorter strings. So it might be worth asking for removing any length limit on VARCHAR (say 255) instead. That might be more feasible too. Commented Oct 14, 2020 at 8:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.