views:

330

answers:

2

Hi,

I'm performing a little database optimisation at the moment and would like to set the column lengths in my table through JPA. So far I have no problem setting the String (varchar) lengths using JPA as follows:

@Column(unique=true, nullable=false, length=99) 
public String getEmail() {
    return email;
}

However, when I want to do the same for a column which is of type Long (bigint), it doesn't work. For example, if I write:

@Id
@Column(length=7)
@GeneratedValue(strategy = GenerationType.AUTO) 
public Long getId() {
    return id;
}

The column size is still set as the default of 20. Are we able to set these lengths in JPA or am I barking up the wrong tree?

Thanks, Gearoid.

A: 

precision, scale make more sense for a numeric type. Also depends whether the JDBC driver and RDBMS allows setting of those on the particular column type

DataNucleus
Hmmm, if I set @Column(precision=7) my database client still tells me that it will accept length (or precision) of 20...
Gearóid
No idea what you mean "it tells you". These values are to be used by the JPA implementation to *create* the table. So it ought to generate a DDL statement using them (assuming the JDBC driver allows it). The JPA implementation will tell you what DDL is being used (in the log)
DataNucleus
A: 

Are you using Eclipse as your IDE? If so, I suggest you make use of the Dali plugin (already installed) and active the "JPA Details" view. This view will help guide you as to what attributes you need to plug into your annotations.

You could have scale=7 instead of length=7. However JPA is still going to tell the database to prepare to hold an int of scale 20.

Also make sure you have your database dialect properly set.

HDave