当数据库表已填充JPA时如何正确设置@Id字段
我的数据库中有一个表,并且我在该字段上添加了 @Id
属性。作为策略
,我使用GenerationType.IDENTITY
。当数据库表尚未由 SQL 脚本中的行填充时,此方法可以正常工作。
当表中已经有一些行时,如何设法让它工作?因为当我尝试从应用程序中预填充实体时它不起作用。
我使用 Derby 数据库来实现此目的,并使用 eclipselink 作为实现。
I am having a single table in my database and I have added the @Id
attribute over the field. As strategy
I use GenerationType.IDENTITY
. This works fine WHEN the database table is not already populated by rows from a SQL script.
How can I manage to get it to work when the table already has some rows in it? Because it doesn't work when I am trying to insert entities from my application when it is pre-populated.
I am using Derby database for this and eclipselink as implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用序列生成器从序列表中获取其 id。像这样的事情:
将initial_value设置为大于从脚本填充的行的最大id的值。
从 java 6 ee api:http://download.oracle。 com/javaee/6/api/javax/persistence/TableGenerator.html
Use sequence generator that gets its ids from a sequence table. Something like this:
Set the initial_value to a value larger than the maximum id of the rows populated from the script.
From the java 6 ee api: http://download.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html
我假设您的错误是因为您为表创建的 IDENTITY (例如)以 "1" 开头,并且您已经使用 id "1" 的行填充了表。所以肯定存在 PK 约束违规。如果是这种情况,我可以建议:
这应该在 Derby DB 中工作
I will assume that your error is because the IDENTITY that you has created for you table starts (by example) in "1" and you have already populated the table with a row with the id "1" . So sure exist a PK Constraint violation. If this is the scenario I can suggest:
This should work in Derby DB