What can i do so JPA (i use Hibernate) creates Columns with Unsigned types? Currently all my ID columns are signed.
Asked
Active
Viewed 1.5k times
10
-
3Unsigned SQL types, or Java types? Java does not have native unsigned number types. If you want to use an unsigned SQL type, you have to map it to a sufficiently-large (signed) Java type. For example, an unsigned 32-bit integer type would have to be stored in a (signed) Java `long`. – Matt Ball Mar 10 '11 at 14:32
-
I want to use unsigned SQL types. – Laures Mar 10 '11 at 14:35
-
1Then the answer is to map those to Java `long` fields. – Matt Ball Mar 10 '11 at 14:36
-
i know, i'm looking for ways to have hibernate create the unsigned columns for me. By default hibernate maps my (signed) java long to a signed Bigint(20). I want to make the column unsigned int(11). – Laures Mar 10 '11 at 14:40
-
How are you configuring the mappings? XML or annotations? Are you using Hibernate to generate your schema? – Matt Ball Mar 10 '11 at 14:43
-
I'm using jpa annotations. I want to avoid hibernate annotations if possible – Laures Mar 10 '11 at 14:45
-
my tables are created by my jpa provider (hibernate). – Laures Mar 10 '11 at 15:05
3 Answers
20
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.

Matt Ball
- 354,903
- 100
- 647
- 710
-
8In MySQL the word "UNSIGNED" should come **after** "INT(11)". – naXa stands with Ukraine Jan 24 '17 at 13:31
0
In MySQL Workbench "UNSIGNED" should come after "INT(11)"

chimpui
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 18 '22 at 20:57