Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned Int in JPA and Hibernate

Tags:

hibernate

jpa

What can i do so JPA (i use Hibernate) creates Columns with Unsigned types? Currently all my ID columns are signed.

like image 772
Laures Avatar asked Mar 10 '11 14:03

Laures


People also ask

Can we use both JPA and hibernate?

You cant actually use both of them in the same application. For backwards compatibility. We are expanding our application and want to start using Spring Data JPA, but still keep the old hibernate implementation. Its better you develop your new application as a separate microservice and use spring data jpa ..

Can we create JPA entity without ID?

If your object does not have an id, but its table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it. Save this answer.

Is @column mandatory in JPA?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.


1 Answers

Using the columnDefinition property on the @Column annotation should do it. Taking a total guess at the SQL type you're going for:

private long foo;

@Column(columnDefinition = "UNSIGNED INT(11)")
public long getFoo()
{
    return foo;
}

N.B. Not all databases (like SQL Server, I think) support unsigned int types.

like image 155
Matt Ball Avatar answered Oct 19 '22 01:10

Matt Ball