如何将javax.persistence.Column定义为Unsigned TINYINT? [英] How can a javax.persistence.Column be defined as an Unsigned TINYINT?

查看:189
本文介绍了如何将javax.persistence.Column定义为Unsigned TINYINT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于MySQL数据库中的现有表创建Java持久性实体Bean(使用NetBeans IDE 8.0.1).我遇到了此表中的一个字段,该字段的类型为"Unsigned TINYINT(3)" .我发现可以通过以下操作将列的类型定义为无符号int :

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

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

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.

所以我的问题是:如何将我的列(字段)定义为未签名的TINYINT?

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

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>

推荐答案

经过更多的实验后,我找到了答案.

After experimenting some more I found the answer.

单词"UNSIGNED" 应该在"TINYINT" 之后,而不是之前.该字段应定义如下:

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.

这篇关于如何将javax.persistence.Column定义为Unsigned TINYINT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆