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

Even though I like the idea of overriding the database platform, I would still be interested if it was possible to hook into where the mapping of internal types happen. It would be smart if the column name could be taken into account, so if your column name ends with _pct (percentage) you automatically get scale=5, precision=2) and if it ends with _amt (amount) or _date, it would use different settings based on current project requirements.

Again, this is convention over configuration and it may be going against the spirit of JPA.

Best regards Jens

On Fri, Feb 26, 2016 at 3:36 PM, Jens Teglhus Møller <djarnis@xxxxxxxxx> wrote:
Yeah, I guess that would be a nice solution, will definitely look into that, thanks.

Best regards Jens

On Fri, Feb 26, 2016 at 3:29 PM, andrei ilitchev <andrei.ilitchev@xxxxxxxxxx> wrote:
I would override the database platform:

public class MyPlatform extends Oracle11Platform {
    @Override
    protected Hashtable buildFieldTypes() {
...

The custom platform class must be listed in persistence unit properties (in persistence.xml):
      <property name="eclipselink.target-database" value="mypackage.MyPlatform"/>

Thanks,
Andrei


On 2/26/2016 9:19 AM, Jens Teglhus Møller wrote:
Hi Thomas

Yes, I could do that, but I was looking for a way to do convention over configuration (it seems so modern these days). All the data in my model follow a common pattern and I was looking for a way to apply some common defaults so I would only have to annotate the special cases.

But if that is not possible I will annotate (in xml).

Best regards Jens

On Fri, Feb 26, 2016 at 2:40 PM, Thomas Meyer <thomas@xxxxxxxx> wrote:
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

_______________________________________________
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



_______________________________________________
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


_______________________________________________
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