java Hibernate - 如何提供到整数类型的正确映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12149874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:47:39  来源:igfitidea点击:

Hibernate - How to provide right mapping to integer type?

javamysqlhibernatemaven

提问by Comic Sans MS Lover

I'm executing my maven build and it throws this exception:

我正在执行我的 Maven 构建并抛出此异常:

Last cause: Wrong column type in x.clients for column type. Found: tinyint, expected: integer

Last cause: Wrong column type in x.clients for column type. Found: tinyint, expected: integer

I'm mapping like this:

我是这样映射的:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

And I'm creating the column using InnoDB like this: id int NOT NULL UNIQUE AUTO_INCREMENT

我正在使用 InnoDB 创建列,如下所示: id int NOT NULL UNIQUE AUTO_INCREMENT

Shouldn't this be ok? Why is it saying that he is finding tinyint?

这不应该好吗?为什么说他正在寻找tinyint?

采纳答案by Ian Dallas

Use @Basicfor basic integers. You can always trying declaring your ID as a Long though. I usually always use Long for my IDs. See Mapping Identifier Properities:

对基本整数使用@Basic。不过,您始终可以尝试将您的 ID 声明为 Long。我通常总是使用 Long 作为我的 ID。请参阅映射标识符属性

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

回答by Pedro Ná?ez

I know this question is (really!) old, but:

我知道这个问题(真的!)很老,但是:

TINYINT represents 8-bit values. It's mapped to byte/Byte. It has a minimum value of -128 and a maximum value of 127 (inclusive) in both cases.

TINYINT 表示 8 位值。它映射到字节/字节。在这两种情况下,它的最小值为 -128,最大值为 127(含)。

SMALLINT represents 16-bit values. It's mapped to short/Short.

SMALLINT 表示 16 位值。它映射到短/短。

INTEGER represents 32-bit values. It's mapped to int/Integer.

INTEGER 表示 32 位值。它被映射到 int/Integer。

BIGINT represents 64-bit values. It's mapped to long/Long.

BIGINT 表示 64 位值。它被映射到 long/Long。

So, you can't map a tinyint using an Integer; you must use a Byte.

因此,您不能使用 Integer 映射 tinyint;你必须使用一个字节。

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.htmlhttp://dev.mysql.com/doc/refman/5.7/en/integer-types.html

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html http://dev.mysql.com/doc/refman/5.7/en/integer-types.html

回答by zakmck

I've just had this problem and it should be useful to share the details. What was happening in my case is:

我刚刚遇到了这个问题,分享详细信息应该很有用。在我的情况下发生的事情是:

  • you define @Column (... definition = "integer" )
  • Oracle translated 'integer' into decimal(0,-127) (or decimal(0), I haven't it clear)
  • You use some other application where 'test.hibernate.hbm2ddl.auto' is 'validate'
  • Hibernate reports that integer != decimal and validation fails
  • 你定义 @Column (... definition = "integer" )
  • Oracle 将'整数' 翻译成十进制(0,-127)(或十进制(0),我还不清楚)
  • 您使用其他一些应用程序,其中“test.hibernate.hbm2ddl.auto”是“validate”
  • Hibernate 报告 integer != decimal 和验证失败

my only solution has been to keep test.hibernate.hbm2ddl.auto empty (undefined = the DB is not touched). Not sure whether it's an Oracle or Hibernate bug (or both).

我唯一的解决方案是将 test.hibernate.hbm2ddl.auto 保持为空(未定义 = 未触及数据库)。不确定它是 Oracle 还是 Hibernate 错误(或两者兼有)。

回答by Sal

Add this to the annotations for the idfield:

将此添加到id字段的注释中:

@Column(columnDefinition = "TINYINT")

回答by bluesman

@Column(columnDefinition = "int")

回答by JamesB

Try adding the type annotation:

尝试添加类型注释:

@Type(type = "org.hibernate.type.IntegerType")