I’m trying to use OpenJPA to insert some entries in the database and I’m getting a strange number of @UPDATE@s beside the @INSERT@s.
I isolated the problem to the following snippet of code
1 2 3 4 5 6 7 8 9 10 11 | |
The generated SQL looks like this:
INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 1, (int) 0, (null) null] INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 2, (int) 1, (null) null] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1] INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 3, (int) 2, (null) null] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1] INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 4, (int) 3, (null) null] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 3] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1] INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 5, (int) 4, (null) null] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 3] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 4] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1] INSERT INTO MYTABLE (ID, VALUE, CREATED) VALUES (?, ?, ?) [params=(int) 6, (int) 5, (null) null] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 3] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 4] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 2] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 5] UPDATE MYTABLE SET CREATED = ? WHERE ID = ? [params=(null) null, (int) 1] ...
pay attention to the extra UPDATE statements after each INSERT, the number of extra @UPDATE@s grows as well. From zero @UPDATE@s after the first INSERT, one in the second, two in the third, and so on. I don’t need to say that this is of course really inefficient. I don’t know what is ultimate cause of this but this started when I added a @Temporal(TemporalType.TIMESTAMP) annotation to the entity class. And I can make it go away by calling EntityManager.clear() after each em.getTransaction().commit()
UPDATE: Ok I found a bug report stating that this behaviour is only observed when the entity classes are not enhanced. . So the best solution is use the enhancer. But it really doesn’t work for me, I still get the extra UPDATEs even with enhanced classes. So I’m stuck with the . If the class is PROPERLY ENHANCED the problem goes away.EntityManager.clear() for now.
Check the logs (enable them with <property name="openjpa.Log" value="DefaultLevel=TRACE"/> in persistence.xml) and make sure that you don’t see any entry like
5968 persistencexmltest1PU INFO [main] openjpa.Enhance - Creating subclass for "[class com.rubenlaguna.MyEntity]". This means that your application will be less efficient and will consume more memory than it would if you ran the OpenJPA enhancer. Additionally, lazy loading will not be available for one-to-one and many-to-one persistent attributes in types using field access; they will be loaded eagerly instead.
If you see the Creating subclass for message means that the class wasn’t enhanced, as I read OpenJPA really need enhanced classes, un – enhanced are just for testing and trivial examples.+
For reference:
persistence.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
MyEntity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
References: