如何将javax.persistence.Column定义为Unsigned TINYINT?

时间:2014-10-13 13:24:55

标签: java mysql jpa unsigned tinyint

我正在基于MySQL数据库中的现有表创建Java持久性实体Bean(使用NetBeans IDE 8.0.1)。我在这个表格中遇到了一个类型" Unsigned TINYINT(3)" 的字段。我发现可以对define the type of a column as an unsigned int进行以下操作:

private long foo;

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

重现问题的步骤:

我正在尝试按如下方式创建一个字段:

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

问题:

将项目部署到服务器时收到以下错误:

{"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"}}

但是,如果我从columnDefinition删除" UNSIGNED" (因此该行变为columnDefinition="TINYINT(3) default '40'")我的项目部署成功。所以似乎" UNSIGNED" 无法识别。

所以我的问题是:如何将我的列(字段)定义为无符号TINYINT?

更多细节:

我不确定它是否重要,但我的 persistence.xml 文件如下所示:

<?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>

1 个答案:

答案 0 :(得分:6)

经过多次试验后,我找到了答案。

单词&#34; UNSIGNED&#34; 应该在&#34; TINYINT&#34; 之后而不是之前。该字段应定义如下:

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

我不知道为什么会这样,我只是通过反复试验才发现这一点。也许其他人可以为此提供参考。