OpenJPA 不是数字 ID

发布于 2024-11-29 04:34:34 字数 4091 浏览 1 评论 0原文

OpenJPA 中是否可以有一个字符串 Id?

当我查看文档时,看起来这是可能的,但是当我尝试时,我得到了这个异常:

the given value "86272843-bb2b-4dd4-bb98-11195bbcf04f" cannot be converted into an identity for "class com.dooapp.infiltrea.model.SimpleEntity".  The value is the wrong type (java.lang.String).
<openjpa-2.1.1-r422266:1148538 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: The given value "86272843-bb2b-4dd4-bb98-11195bbcf04f" cannot be converted into an identity for "class com.dooapp.infiltrea.model.SimpleEntity".  The value is the wrong type (java.lang.String).
at org.apache.openjpa.kernel.BrokerImpl.newObjectId(BrokerImpl.java:1278)
at org.apache.openjpa.kernel.DelegatingBroker.newObjectId(DelegatingBroker.java:316)
at org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:485)
at com.dooapp.infiltrea.model.io.ManagerIOImpl.read(ManagerIOImpl.java:112)
at com.dooapp.infiltrea.model.io.ManagerIOTest.testDelete(ManagerIOTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at $Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
Caused by: java.lang.NumberFormatException: For input string: "bb2b-4dd4-bb98-11195bbcf04f"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:410)
at java.lang.Long.parseLong(Long.java:468)
at org.apache.openjpa.util.Id.<init>(Id.java:109)
at org.apache.openjpa.util.Id.newInstance(Id.java:48)
at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.newDataStoreId(JDBCStoreManager.java:808)
at org.apache.openjpa.kernel.DelegatingStoreManager.newDataStoreId(DelegatingStoreManager.java:156)
at org.apache.openjpa.kernel.BrokerImpl.newObjectId(BrokerImpl.java:1253)

我只是

@Id
String id;


@PrePersist
public void generateUUID() {
    this.setId(UUID.randomUUID().toString());
}

当我删除 String 并放置 long 时,它工作得很好,但这不是我想要的。

这是 JPA 上的错误,还是我做错了什么?

谢谢

Is it possible to have a String Id in OpenJPA?

When I look at the documentation, it look's like it's possible, but when I try, I get this exception :

the given value "86272843-bb2b-4dd4-bb98-11195bbcf04f" cannot be converted into an identity for "class com.dooapp.infiltrea.model.SimpleEntity".  The value is the wrong type (java.lang.String).
<openjpa-2.1.1-r422266:1148538 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: The given value "86272843-bb2b-4dd4-bb98-11195bbcf04f" cannot be converted into an identity for "class com.dooapp.infiltrea.model.SimpleEntity".  The value is the wrong type (java.lang.String).
at org.apache.openjpa.kernel.BrokerImpl.newObjectId(BrokerImpl.java:1278)
at org.apache.openjpa.kernel.DelegatingBroker.newObjectId(DelegatingBroker.java:316)
at org.apache.openjpa.persistence.EntityManagerImpl.find(EntityManagerImpl.java:485)
at com.dooapp.infiltrea.model.io.ManagerIOImpl.read(ManagerIOImpl.java:112)
at com.dooapp.infiltrea.model.io.ManagerIOTest.testDelete(ManagerIOTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at $Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
Caused by: java.lang.NumberFormatException: For input string: "bb2b-4dd4-bb98-11195bbcf04f"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:410)
at java.lang.Long.parseLong(Long.java:468)
at org.apache.openjpa.util.Id.<init>(Id.java:109)
at org.apache.openjpa.util.Id.newInstance(Id.java:48)
at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.newDataStoreId(JDBCStoreManager.java:808)
at org.apache.openjpa.kernel.DelegatingStoreManager.newDataStoreId(DelegatingStoreManager.java:156)
at org.apache.openjpa.kernel.BrokerImpl.newObjectId(BrokerImpl.java:1253)

I just have

@Id
String id;


@PrePersist
public void generateUUID() {
    this.setId(UUID.randomUUID().toString());
}

When I remove String and put long instead, it work well, but it's not what I want.

Is this a bug on JPA, or am I doing something wrong?

Thank's

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑寡妇 2024-12-06 04:34:34

OpenJPA 允许您选择 uuid id - 看这里

OpenJPA allows you to choose uuid ids - see here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文