sachit aggarwal
sachit aggarwal

Reputation: 21

Change Datatype for Table Column Generated by JPA

  1. Is it possible to change column length for a table generated using JPA? I will like to increase length for this field
@NotNull
@Column(name = "ShortDescription", length = 65)
private String shortDescription;
  1. E.g. if I change the length to 80 and redeploy and will column get updated?

Upvotes: 1

Views: 1366

Answers (1)

Abhijeet
Abhijeet

Reputation: 4309

For EclipseLink

EclipseLink does not support the automatic update of existing columns. You have to update it manually using DDL query or you can use one of the following options according to your requirements.

create-tables: EclipseLink will attempt to execute a CREATE TABLE SQL for each table. If the table already exists, EclipseLink will follow the default behavior for your specific database and JDBC driver combination (When a CREATE TABLE SQL is issued for an already existing table). In most cases an exception is thrown and the table is not created; the existing table will be used. EclipseLink will then continue with the next statement.

create-or-extend-tables: EclipseLink will attempt to create tables. If the table exists, EclipseLink will add any missing columns.

drop-and-create-tables: EclipseLink will attempt to DROP all tables, then CREATE all tables. If any issues are encountered. EclipseLink will follow the default behavior for your specific database and specific JDBC driver combination, then continue with the next statement. This is useful in development if the schema frequently changes or during testing when an existing data needs to be cleared.

none: default, no ddl generated; no schema generated.

Add this property in your persistence.xml file.

For Hibernate

The values create, create-drop, validate, and update basically influence how the schema tool management will manipulate the database schema at startup.

You can use update value for hibernate.hbm2ddl.auto so that your column length will get updated on application startup. Add this property in your persistence.xml file.

Upvotes: 1

Related Questions