开发者

OpenJPA HSQLdb - how to handle IDs

开发者 https://www.devze.com 2023-01-01 14:37 出处:网络
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:

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(Inte开发者_高级运维ger 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


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

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消