OpenJPA HSQLdb-如何处理ID [英] OpenJPA HSQLdb - how to handle IDs

查看:88
本文介绍了OpenJPA HSQLdb-如何处理ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用OpenJPA和HSQLdb处理数据库表的ID时遇到麻烦.我创建了一个Abstract类,在其中处理注释和东西以重新映射到数据库中:

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;
    }

这是用于创建Testobjekts的外观.

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()); 

尽管如此,我仍然设置... 1,什么也没有...我得到了错误. 像:

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.

我希望ORM层能够处理这些ID信息而不会打扰我.我对Hibernate的经验是可以很好地处理这些东西...但是OpenJPA在这里似乎很麻烦.我认为我的注释有误或有什么问题,但在跟踪此多层问题时遇​​到了麻烦.

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.

我在persistence.xml中配置了OpenJPA:

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>

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

How do I handle an automated ID strategy with OpenJPA?

谢谢, 希望

推荐答案

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

How do I handle an automated ID strategy with OpenJPA?

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

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;
}

这篇关于OpenJPA HSQLdb-如何处理ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆