Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Finetuning data types for model generation - override defaults

Hi Jens,

I think the correct way to do this in Jpa is to use @Column and scale and precision attributes. Same for Date fields, where you can control the mapping with @Temporal.

You may also want to consider the bean validation annotations which are a also used to some extend by the JPA provider.

With kind regards
Thomas


With kind regards
Thomas

Am 26.02.2016 um 13:20 schrieb Jens Teglhus Møller <djarnis@xxxxxxxxx>:

Just a bit more background.

My entity classes are autogenerated pojo's (from a json schema, not that it matters). I don't have a lot of control over the data types (I can have String, Long, Double, java.util.Date).

I use an orm.xml file to define the mapping but would like convention over configuration for the type mapping.

All my date related values are have second precision but since they are stored in a java,util.Date the default type mapping to oracle is TIMESTAMP(6), which is with micro-second precision.

I have managed to have EclipseLink create columns with oracle data type DATE by adding the following converter to my project:

@Converter(autoApply = true)
public class DateConverter implements AttributeConverter<java.util.Date, java.sql.Date> {

@Override
public java.sql.Date convertToDatabaseColumn(java.util.Date attribute) {
if (attribute == null) {
return null;
}
return new java.sql.Date(attribute.getTime());
}

@Override
public java.util.Date convertToEntityAttribute(java.sql.Date dbData) {
if (dbData == null) {
return null;
}
return dbData;
}
}

I got the clue by looking at the source code for OraclePlatform.buildFieldTypes() and was pleasantly surprised when I discovered that the converter was also taken into account when generating the schema.

But, it is a bit of a hack and my luck runs out if I want to map java.lang.Long to NUMBER(9), If I could live with NUMBER(10) I could create a similar converter with <Long, Integer> signature. So is there a different way to do it (that does not involve setting scale for all the fields)?

Best regards Jens

On Thu, Feb 25, 2016 at 4:38 PM, Jens Teglhus Møller <djarnis@xxxxxxxxx> wrote:
Hi

Is it in anyway possible to override the default data types when generating the data model?

I have a set of pojo's with Long fields and I know they will fit in a certain type (like oracles number(10)).

The default is number(38), I know I can annotate with scale and precision but it would be nice if I could override the defaults.

Same thing for dates and varchars etc.

I have been looking a bit at the documentation and javadocs but did not find anything.

I don't mind having to implement it in a class.

Can it be done?

Best regards Jens

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top