JPA:InvalidStateException 错误 +复合密钥/EmbeddableId/序列生成的 ID
我目前使用 OpenJPA 在数据库和 Java 应用程序中设置了一个模式,该模式在大多数情况下都可以工作,但有时我会收到一些用户的错误:
org.apache.openjpa.persistence.InvalidStateException:尝试设置列“table1.1”。 ID”到两个不同的值
table1 实际上有一个复合键(两个值),并且该键中的每个值都是另一个表的外键。我使用 RSA(Rational Software Architect)来为我设置实体(生成的代码)。它在 table1 的实体类中设置了一个 PK 类(使用 @EmbeddableId 引用 PK 类),然后在同一个 table1 实体类中(以及这些列引用的实体类中)设置了两个 @ManyToOne 关系,因为它们是外键
现在,正如我上面提到的,组合键中的每个值都是一个外键。好吧,每个外键实际上都是使用它们自己的实体类中的外部 Sequencer 生成的。我正在使用 DB2 并在列上使用 @GenerateValue(即 table2 和 table3 实体类中的 ID)。我也对每个使用strategy=GenerationType.SEQUENCE。
再说一次,一切正常,但不是 100% 有效,我不确定为什么。我已经通过清除所有内容并重置序列生成器来摆脱此错误,但我知道这绝对不是解决方案。这是否与数据库中的两个复合键值是使用序列生成的列的外键但 PK 实体可能不知道这一事实有关?
我还注意到,它仅适用于在 Users 表中拥有记录的用户(上面提到的外键之一是 Users 表,而另一个 FK 是另一个表)。会发生什么情况,如果用户不在表中,它会创建一个,如下所示:
User newUser = userManager.getNewUser();
newUser.setName(..);
newUser.setEmail(..);
...
完成后,我上面提到的 PK 类将创建一个新实例,然后将其调用到另一个表中。上面用户的ID被传递到PK中。比如:
PK newPK = pkManager.getNewPK();
newPk.setAID(newUser.getID());
有人遇到过这种情况吗?有什么解决办法吗?
I currently have a schema set up with my database and Java Application using OpenJPA that works most of the time, but sometimes I get the error for a few users:
org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "table1.ID" to two different values
table1 actually has a composite Key (two values) and each value in that key is a foreign key to another table. I used RSA (Rational Software Architect) to set up the entities for me (generated code). It set up a PK class (using @EmbeddableId to reference the PK class) in the Entity class for table1, and then two @ManyToOne relationships in the same table1 Entity class (and also in the entity classes that those columns reference) since they are foreign keys
Now, as I mentioned above, each value in the composite key is a foreign key. Well, each of those foreign keys is actually generated using an outside Sequencer in their own entity classes. I am using DB2 and using @GeneratedValue on the columns (i.e. the IDs in table2's and table3's entity classes). I use strategy=GenerationType.SEQUENCE also for each.
Again, everything works USUALLY but not 100% of the time and I'm unsure why. I have gotten rid of this error by wiping out everything and resetting the Sequence Generators, but I know this is definitely not a solution. Could it have something to do with the fact that the two Composite Key values in the database are foreign keys to columns which were generated using a sequence, but the PK entity might not know?
I have noticed too that it only works for users who have a record in the Users table (one of the foreign keys mentioned above is to a Users table, while the other FK is to another table). What happens, if a user is not in the table, it creates one, something like:
User newUser = userManager.getNewUser();
newUser.setName(..);
newUser.setEmail(..);
...
When it's done, the PK class I mentioned above has a new instance of that created, which is then called into another table. The ID from the user above is passed into the PK. Like:
PK newPK = pkManager.getNewPK();
newPk.setAID(newUser.getID());
Has anybody run into this? Any solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,已解决问题。我检查了我的代码,意识到我忘记重构一行代码(数据模型的更改)。
Sorry, fixed the problem. I went through my code and realized I had forgot to refactor one line of code (change in data model).