Search code examples
javamysqljpaunsignedtinyint

How can a javax.persistence.Column be defined as an Unsigned TINYINT?


I am creating a Java Persistence Entity Bean (with NetBeans IDE 8.0.1) based on an existing table in a MySQL database. I've come across a field in this table which is of type "Unsigned TINYINT(3)". I have found that the following can be done to define the type of a column as an unsigned int:

private long foo;

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

Steps to reproduce the problem:

I am trying to create a field as follows:

@Size(max = 3)
@Column(name = "WorkingHours", columnDefinition="UNSIGNED TINYINT(3) default '40'")
private Integer workingHours;

The problem:

I receive the following error when deploying my project to my server:

{"JBAS014671: Failed services" => {"jboss.persistenceunit.\"my-project.ear/my-project-ejb.jar#old-db\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"my-project.ear/my-project-ejb.jar#old-db\": javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [create table ... etc.]
    Caused by: javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [create table ... etc.]
    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNSIGNED TINYINT(3), ... etc.' at line 1"}}

If I however remove "UNSIGNED" from my columnDefinition (so the line becomes columnDefinition="TINYINT(3) default '40'") deployment of my project is successful. So it seems that "UNSIGNED" is not recognized.

So my question is: how do I define my column (field) as an unsigned TINYINT?

Some more details:

I'm not sure if it is important, but my persistence.xml file looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="old-db" transaction-type="JTA">
    <jta-data-source>java:/jboss/datasources/mySQL_pool_old</jta-data-source>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

Solution

  • After experimenting some more I found the answer.

    The word "UNSIGNED" should come after "TINYINT" instead of before it. The field should be defined as follows:

    @Size(max = 3)
    @Column(name = "WorkingHours", columnDefinition="TINYINT(3) UNSIGNED default '40'")
    private Integer workingHours;
    

    I am not sure why this is, I have found this out only through trial and error. Maybe someone else can provide a reference for why this is.