OpenJPA HSQLdb - 如何处理 ID
我在使用 OpenJPA 和 HSQLdb 处理数据库表的 ID 时遇到问题。我创建了一个抽象类,在其中处理注释和要重新映射到数据库中的内容:
// Property accessors
@Id
@Column(name = "IDTESTOBJEKT", unique = true, nullable = false)
public Integer getIdtestobjekt() {
return this.idtestobjekt;
}
public void setIdtestobjekt(Integer idtestobjekt) {
this.idtestobjekt = idtestobjekt;
}
它作为用于创建 Testobjekts 的外观。
Testobjekt test_obj = new Testobjekt();
test_obj.setEigentuemerin("helge");
// test_obj.setIdtestobjekt(1);
EntityManagerHelper.beginTransaction();
TestobjektDAO test_dao = new TestobjektDAO();
test_dao.save(test_obj);
EntityManagerHelper.commit();
List<Testobjekt> foo;
foo = test_dao.findByEigentuemerin("helge");
Testobjekt from_db = foo.get(0);
System.out.println(from_db.getEigentuemerin());
不过我设置的... 1,什么都没有...我收到错误。 比如:
Field "model_layer.AbstractTestobjekt.idtestobjekt" of "model_layer.Testobjekt@3209fa8f" can not be set to "null" value.
我希望 ORM 层能够处理 ID 的东西而不打扰我。我对 Hibernate 的经验是它可以很好地处理这些事情......但 OpenJPA 在这里似乎很麻烦。我认为我的注释是错误的或其他什么,但我无法追踪这个多层问题。
我在 persistence.xml 中配置了 OpenJPA:
<persistence-unit name="HSQLdb_mvn_openJPA_autoTablesPU"
transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>model_layer.Testobjekt</class>
<class>model_layer.AbstractTestobjekt</class>
<properties>
<property name="openjpa.ConnectionDriverName"
value="org.hsqldb.jdbc.JDBCDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:hsqldb:hsql://localhost:9001/mydb" />
<property name="openjpa.ConnectionUserName" value="SA" />
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
如何使用 OpenJPA 处理自动 ID 策略?
谢谢, 威希
I'm having trouble handling IDs of my databse tables using OpenJPA and HSQLdb. I created an Abstract class where I handle annotations and stuff to remap into the DB:
// Property accessors
@Id
@Column(name = "IDTESTOBJEKT", unique = true, nullable = false)
public Integer getIdtestobjekt() {
return this.idtestobjekt;
}
public void setIdtestobjekt(Integer idtestobjekt) {
this.idtestobjekt = idtestobjekt;
}
It's as a Facade used to create Testobjekts.
Testobjekt test_obj = new Testobjekt();
test_obj.setEigentuemerin("helge");
// test_obj.setIdtestobjekt(1);
EntityManagerHelper.beginTransaction();
TestobjektDAO test_dao = new TestobjektDAO();
test_dao.save(test_obj);
EntityManagerHelper.commit();
List<Testobjekt> foo;
foo = test_dao.findByEigentuemerin("helge");
Testobjekt from_db = foo.get(0);
System.out.println(from_db.getEigentuemerin());
Nevertheless what I set ... 1, nothing... I get errors.
Like:
Field "model_layer.AbstractTestobjekt.idtestobjekt" of "model_layer.Testobjekt@3209fa8f" can not be set to "null" value.
I want the ORM layer to handle that ID stuff without bothering me. My experience with Hibernate is that is handles that stuff quite well... but OpenJPA seems to be cumbersome here. I assume my annotations are wrong or something but I'm having trouble tracking this multi-layered issue down.
I configured OpenJPA in the persistence.xml:
<persistence-unit name="HSQLdb_mvn_openJPA_autoTablesPU"
transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>model_layer.Testobjekt</class>
<class>model_layer.AbstractTestobjekt</class>
<properties>
<property name="openjpa.ConnectionDriverName"
value="org.hsqldb.jdbc.JDBCDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:hsqldb:hsql://localhost:9001/mydb" />
<property name="openjpa.ConnectionUserName" value="SA" />
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
</properties>
</persistence-unit>
How do I handle an automated ID strategy with OpenJPA?
Thanks,
wishi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 @GenerateValue 注释(我建议使用默认的 GenerationType.AUTO 策略,该策略指示持久性提供程序应为特定数据库选择适当的策略嗯>):
Use the
@GeneratedValue
annotation (and I suggest using the defaultGenerationType.AUTO
strategy which indicates that the persistence provider should pick an appropriate strategy for the particular database):