10

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

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Laures
  • 5,389
  • 11
  • 50
  • 76
  • 3
    Unsigned 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
  • 1
    Then 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 Answers3

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
0

In MySQL Workbench "UNSIGNED" should come after "INT(11)"

  • 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
0

MySQL has excellent documentation addressing the same problem. Link

If you are creating tables using DDLs (manually or using DB migration tools) and only want to understand which Java data type best suits your MySQL Unsigned Int column, the answer is long/Long.

vvs14
  • 720
  • 8
  • 19