ObjectDB ObjectDB

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

edit
delete
#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
edit
delete
#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.

edit
delete
#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?

edit
delete
#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
edit
delete
#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?

 

 

edit
delete
#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
edit
delete

Reply

To post on this website please sign in.