相关疑难解决方法(0)

在JPA/hibernate中映射UUID的问题

根据文档,hibernate 3.6应该支持java.util.UUID类型.但是当我将其映射为:

@Id protected UUID uuid;
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [test-applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: persistenceUnit] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:529) ~[spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:495) ~[spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:656) ~[spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:629) ~[spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) ~[spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:338) ~[spring-orm-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    ... …
Run Code Online (Sandbox Code Playgroud)

java uuid hibernate jpa

27
推荐指数
4
解决办法
5万
查看次数

如何将.NET Guid读入Java UUID

我需要将在.NET中生成的Guid传递给Java应用程序.我用Guid.ToByteArray()它将它作为a存储在磁盘上byte[],然后将其读入Java并将其转换为UUID.为此,我复制了UUID的(私有)构造函数的实现,该构造函数采用byte[]:

private UUID(byte[] data) {
    long msb = 0;
    long lsb = 0;
    assert data.length == 16;
    for (int i=0; i<8; i++)
        msb = (msb << 8) | (data[i] & 0xff);
    for (int i=8; i<16; i++)
        lsb = (lsb << 8) | (data[i] & 0xff);
    this.mostSigBits = msb;
    this.leastSigBits = lsb;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我使用UUID检查时toString(),Java UUID与.NET Guid不同.

例如,.NET Guid

888794c2-65ce-4de1-aa15-75a11342bc63
Run Code Online (Sandbox Code Playgroud)

变成Java UUID

c2948788-ce65-e14d-aa15-75a11342bc63
Run Code Online (Sandbox Code Playgroud)

似乎前三组的字节顺序是相反的,而后两组中的顺序是相同的.

既然我希望toString()Guid和UUID都能产生相同的结果,有谁知道我应该如何正确地将.NET Guid读入Java UUID?

编辑:澄清一下,实施不是我自己的.它是类的私有构造函数,java.util.UUID它采用a byte[] …

.net c# java uuid guid

20
推荐指数
5
解决办法
1万
查看次数

UUID疯狂与mssql

我的数据库条目具有值的UUID(使用Microsoft SQL Server Management Studio提取)

CDF86F27-AFF4-2E47-BABB - 2F46B079E98B

将其加载到我的Scala应用程序后,toString方法会生成此值

276ff8cd-f4af-472e降低-BABB - 2f46b079e98b

这是怎么发生的?当我手边只有裸字符串CDF86F27-AFF4-2E47-BABB-2F46B079E98B时,如何以编程方式创建UUID实例?

相关的Slick代码(前者:表定义,后者:数据库访问对象)

class ChannelTable(tag: Tag) extends Table[ChannelTuple](tag, "Channel") {
  def id = column[UUID]("Id", O.PrimaryKey)
  def channelId = column[Int]("Channel_Id", O.NotNull)
  def timer = column[UUID]("Timer_Id", O.NotNull)
  def from = column[Timestamp]("FromTime", O.NotNull)
  def to = column[Timestamp]("ToTime", O.NotNull)
  def mon = column[Boolean]("Mon", O.NotNull)
  def tues = column[Boolean]("Tues", O.NotNull)
  def wed = column[Boolean]("Wed", O.NotNull)
  def thu = column[Boolean]("Thu", O.NotNull)
  def fri = column[Boolean]("Fri", O.NotNull)
  def sat = column[Boolean]("Sat", O.NotNull) …
Run Code Online (Sandbox Code Playgroud)

sql-server uuid scala

8
推荐指数
1
解决办法
3739
查看次数

使用 PostgreSQL 和 SQL Server 休眠 UUID

我有一个应用程序,我想在 PostgreSQL 和 SQL Server 上运行。我想使用 java.util.UUID 作为 ID。

我已将 SQL Server 中的列定义为

id  UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE
Run Code Online (Sandbox Code Playgroud)

我已将 PostgreSQL 中的列定义为

id  UUID NOT NULL
Run Code Online (Sandbox Code Playgroud)

这些列在我的 JPA 实体中定义为

@Id
@Column(name = "id")
public UUID getId() {
    return id;
}
Run Code Online (Sandbox Code Playgroud)

这适用于 PostgreSQL,因为它将 UUID 传递给 PostgreSQL JDBC 驱动程序。这种方式适用于 SQL Server,因为 Hibernate 在将 UUID 发送到 SQL Server 之前将其转换为其二进制形式。不幸的是,二进制格式略有不同,导致 GUID 的字符串表示(例如,使用 SSMS 查看它们时)不同,这至少是令人困惑的。

这可以在 SQL Server 中通过将列的类型更改为 uuid-char 来解决

@Id
@Type(type = "uuid-char")
@Column(name = "id")
public UUID getId() {
    return …
Run Code Online (Sandbox Code Playgroud)

java sql-server postgresql uuid hibernate

5
推荐指数
2
解决办法
4286
查看次数

标签 统计

uuid ×4

java ×3

hibernate ×2

sql-server ×2

.net ×1

c# ×1

guid ×1

jpa ×1

postgresql ×1

scala ×1