Home » Support » Forum » Is there a function of "drop table" or "delete from table"?

Is there a function of "drop table" or "delete from table"?

#1

Besides the em.remove() one by one, is there a function of "drop table" or "delete from table"?

TIA

Institute of Computer Technologies
#2

You can execute a DELETE query:

    em.createQuery("DELETE FROM MyEntity e").executeUpdate();

This is implemented since version 2.2.4 but the manual has not been updated yet.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Thanks. I never think it is "executeUpdate()", query.getresultlist() cause exceptions.

Yes, you'd better to put it into manual, since "drop table" is often in debugging. I can not find "drop table" in this site BTW.

Institute of Computer Technologies
#4

The code “createQuery("DELETE FROM MyEntity e").executeUpdate();” works, but it can not reset id to zero!

"drop table" or "delete from table" or "truncate" can clear the id counting.

Any idea?

Institute of Computer Technologies
#5

Resetting generated ID sequences is not supported by JPA and JDO.

The following non portable code, however, resets the global sequence IDs:

    javax.jdo.datastore.Sequence sequence =
        em.unwrap(javax.jdo.PersistenceManager.class).getSequence("$auto");
    sequence.allocate((int)-sequence.currentValue());

Assuming you use JPA - you have to use the JDO ability to access sequences. The code above passes a negative allocation request to the allocate method in order to reset the current value. It will work only if the ID is still a 32 bit int value since the allocate methods receives an int value.

"$auto" is the name of the automatic global sequence.

The name of an identity sequence (specific per entity class) is entityName + "$identity".

User defined generators (sequence and table) have explicit names.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#6

I agree, would be really useful to be able to clear a database during testing.

Perhaps, also setting a flag during startup to label the database as 'resettable' too.

Additionally, I was wondering if there is any way to get a list of the entity names in the database?

 

 

#7

Clearing the database content is supported since build 2.2.9_05 and there is even a way to label databases as resettable by restricting this risky operation to databases with specified file extensions.

See this forum thread and the explanation of the ;drop connection url parameter in the manual.

About your other question - I will move it to a separate thread. Please follow the posting instructions and use separate threads for unrelated topics. This is essential in order to keep the forum organized and useful to other users.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login