OpenJPA HSQLdb - how to handle IDs

为君一笑 提交于 2019-12-11 06:28:55

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/2950984/openjpa-hsqldb-how-to-handle-ids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!