OpenJPA HSQLdb - 如何处理 ID

发布于 2024-09-03 22:10:28 字数 2149 浏览 4 评论 0原文

我在使用 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 技术交流群。

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

发布评论

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

评论(1

疯到世界奔溃 2024-09-10 22:10:28

如何使用 OpenJPA 处理自动 ID 策略?

使用 @GenerateValue 注释(我建议使用默认的 GenerationType.AUTO 策略,该策略指示持久性提供程序应为特定数据库选择适当的策略嗯>):

@Id 
@GeneratedValue
@Column(name = "IDTESTOBJEKT", unique = true, nullable = false)
public Integer getIdtestobjekt() {
    return this.idtestobjekt;
}

How do I handle an automated ID strategy with OpenJPA?

Use the @GeneratedValue annotation (and I suggest using the default GenerationType.AUTO strategy which indicates that the persistence provider should pick an appropriate strategy for the particular database):

@Id 
@GeneratedValue
@Column(name = "IDTESTOBJEKT", unique = true, nullable = false)
public Integer getIdtestobjekt() {
    return this.idtestobjekt;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文