简体   繁体   English

来自Postgresql的类型和来自Play(异常)的类型

[英]Types from Postgresql and types from Play (anorm)

I have a class representing a model: 我有一个代表模型的班级:

class Payment(
  val id: Pk[Int] = NotAssigned,
  //.....
)

Here is how it is defined in a db: 这是在db中的定义方式:

CREATE TABLE payment
(
  id serial NOT NULL,
  //.........
)

It throws an exception java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer I got such the exception throughout a project pretty often. 它引发异常java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer我在整个项目中经常遇到这样的异常。 I figure it's because I use the wrong data types in Play (anorm) and Postgresql, meaning they don't correspond each other correctly. 我认为这是因为我在Play(anorm)和Postgresql中使用了错误的数据类型,这意味着它们之间没有正确对应。

So where how do I find out what type of Postresql corresponds to type of anorm? 那么,在哪里可以找到与主动脉类型相对应的Postresql类型呢? I found only small peaces of the information and they weren't on the official web sites. 我发现这些信息很少,也没有出现在官方网站上。 It seems like the table of such correspondence doesn't even exist, does it? 这样的对应表似乎根本不存在,对吗?

These are the corresponding types: 这些是对应的类型:

1) Int in scala and integer/serial in postgresql are 32 bits and both go up to 2,147,483,647. 1)scala中的int和postgresql中的inte / serial是32位,并且都达到2,147,483,647。 Postgresql serial is only an auto-incrementing integer. Postgresql序列号只是一个自动递增的整数。

2) Long in scala and bigint/bigserial in postgresql are 64 bits and both go up to 9,223,372,036,854,775,807. 2)scala中的long和postgresql中的bigint / bigserial是64位,并且都达到9,223,372,036,854,775,807。 Postgresql bigserial is only an auto-incrementing bigint. PostgreSQL bigserial只是一个自动递增的bigint。

If you want to use the same types, you can use bigserial/Long or serial/Int. 如果要使用相同的类型,则可以使用bigserial / Long或serial / Int。

However, for our projects, we ended up using Long in Scala and serial in postgresql and this works fine. 但是,对于我们的项目,我们最终在Scala中使用Long并在postgresql中使用了serial,这很好用。 This gives us the freedom to simply change the data type in postgresql from serial to bigserial if required in future. 如果将来需要的话,这使我们可以自由地将postgresql中的数据类型从串行更改为bigserial。

As a side note, we could not see any real advantage for Pk[..] so decided to use Option[Long] instead of Pk[Long] and None instead of NotAssigned. 附带说明一下,我们看不到Pk [..]有任何真正的优势,因此决定使用Option [Long]代替Pk [Long],并选择None代替NotAssigned。 Then you use the generic Option types and will never need to import anorm into your controllers. 然后,您将使用通用的Option类型,并且永远不需要将定理导入到控制器中。

Anorm is DB agnostic, it doesn't care about DB vendor specific type, but about JDBC types. Anorm与数据库无关,它不在乎数据库供应商特定的类型,而在乎JDBC类型。 You can find JDBC type mappings in Oracle doc: http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html#table1 . 您可以在Oracle文档中找到JDBC类型映射: http : //docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html#table1

Anorm included in Play 2.3 (latest release) take care of more numeric conversion. Play 2.3(最新版本)中包含的Anorm需要更多的数字转换。 You can find details at http://applicius-en.tumblr.com/post/87829484643/anorm-whats-new-play-2-3 . 您可以在http://applicius-en.tumblr.com/post/87829484643/anorm-whats-new-play-2-3中找到详细信息。

If you still have conversion issue with latest Anorm, you may add an issue on Play github project. 如果您对最新的Anorm仍然存在转换问题,则可以在Play github项目上添加一个问题。

Best 最好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM