Topics

How to alter a column length


claudio_rosati@...
 

Hello all,

I have defined a java class using the default attributes:

@PersistenceCapable(detachable = "true")
public class PropertyTemplate {
  ...
    private String description;
  ...
}

And stared using the dat storage (based on Apache Derby).
Now I discovered that the default 255 characters for the description property is not enough, and I've changed it on the class in this way:

  @Persistent
  @Column(jdbcType = "VARCHAR", length = 2048)
  private String description;

I was supposing datanucleus would have altered the table automatically at the first access, but this was not the case. So how can I programmaticaly altering the table? I've tried with the following code:

  Query<PropertyTemplate> alterQuery = pm.newQuery(
    javax.jdo.query.SQL",
    ALTER TABLE PROPERTYTEMPLATE MODIFY COLUMN DESCRIPTION VARCHAR (2048)");
 
  alterQuery.setClass(PropertyTemplate.class);
  alterQuery.execute();

but it failed because the query was not a "select" one. Now I don't understand how to proceed.
Any help will be highly appreciated.

Claudio
 


Andy
 
Edited

JDO (and JPA) don't do automatic changes to schema. They do CREATE or DELETE only. Metadata (for schema) is only ever used on a CREATE, and since you already have tables it is up to you to have the tables consistent with your metadata. If a table already exists then it can equally already have data in it. Consequently just issuing ALTER TABLE commands can fail due to this, or can screw up the data already stored ... hence why it is not an automated option

SQL update statements are described in this. If that method listed doesnt work then you as a minimum state the full stack trace + error message, rather than just "it failed".


claudio_rosati@...
 

Thank you Andy for pointing me to the right point in the documentation. The problem arose because I forgot setting the persistence property "datanucleus.query.sql.allowAll" to true. This caused my Alter Table command to fail.