Different column types for different databases with JPA

Is there a posibility to define with JPA different column types depending on the used database?

I need to store the id as uuid and it must be protable. That's the problem. PostgreSQL has 'uuid', MSSQL 'uniqueidentifier' and Oracle has nothing, there must be used 'RAW', I think.

Have anybody an idea and can help me?

Edit: Currently the ids are generated with java.util.UUID and stored in the database as varchar. But because of performance problems with that data type I want to store the ids as a uuid type. For Oracle must use the RAW type because no uuid type exist. How can I tell JPA use uuid type with PostgreSQ/MSSQL and RAW type with Oracle?

Answers


You can use java.util.UUID to generate your uuids and store it into a character varying(60) column type. Now since you want to be a primary key it should be good to auto generate it a persist. So you can add on your class an entity listener at PrePersist:

@EntityListeners({ AbstractEntity.AbstractEntityListener.class})
public class Entity {

    public static class AbstractEntityListener {
        @PrePersist
        public void onPrePersist(Entity entity) {
            entity.uid();
        }
    }

    private String uid() {
        if (uid == null)
          uid = UUID.randomUUID().toString();
        return uid;
    }
}

Note that i didn't test this. So it may not work 100%.


I would use a SequenceGenerator. Check this answer for more details. You will have to make one seq. generator per entity in order to make sure you do not get concurrency problems. Also make sure that you set a good/not existent initial value.


Need Your Help

ASP.NET MVC image from byte array

c# asp.net-mvc model-view-controller

currently I have a byte array representing my Image in my ViewModel. I display it with the following code:

Django Admin redirects to 500 error

django ubuntu nginx django-admin uwsgi

I am getting a 500 error when i login to the django admin interface.