openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henno Vermeulen <>
Subject RE: N+1 select problem for Map no matter what I try
Date Thu, 17 Feb 2011 10:25:56 GMT
Thank you for your reply! I tried and it unfortunately doesn't work. I stripped my example
down to the bare minimum. I am using OpenJPA 2.0.0 (downloaded openjpa-all using Maven).
In my real application I use compile time enhancement and mssql server, I am running this
test with hsqldb and openjpa as javaagent (-javaagent:C:/Users/h.vermeulen/.m2/repository/org/apache/openjpa/openjpa-all/2.0.0/openjpa-all-2.0.0.jar).

Here are my classes and settings:

<persistence xmlns=""
	xsi:schemaLocation=" persistence_1_0.xsd"

	<persistence-unit name="testPU">
			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
			<property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver" />
			<property name="openjpa.ConnectionURL" value="jdbc:hsqldb:mem:test" />
			<property name="openjpa.ConnectionUserName" value="sa" />
			<property name="openjpa.ConnectionPassword" value="" />
			<property name="openjpa.Log" value="DefaultLevel=TRACE" />
package entities;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.apache.openjpa.persistence.PersistentMap;

public class TestEntity {

	private Long id;

	@PersistentMap(elementCascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
	private Map<String, String> strings = new HashMap<String, String>();

	public TestEntity() {

	public Long getId() {
		return id;

	public Map<String, String> getStrings() {
		return strings;

import java.util.List;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.openjpa.persistence.OpenJPAEntityManager;

import entities.TestEntity;

public class MapPerformanceTest {
	private EntityManagerFactory factory;

	public void createEntityManagerFactory() {
		factory = Persistence.createEntityManagerFactory("testPU", System

	private void persistInTransaction(TestEntity item) {
		OpenJPAEntityManager em = (OpenJPAEntityManager) factory

	private void createEntities() {
		for (int i = 0; i < 100; i++) {
			TestEntity entity = new TestEntity();
			entity.getStrings().put("a", "test " + i);
			entity.getStrings().put("b", "another test " + i);

	private List<TestEntity> findAllEntities() {
		OpenJPAEntityManager em = (OpenJPAEntityManager) factory
		String query = "SELECT z FROM " + TestEntity.class.getSimpleName()
				+ " z";
		List<TestEntity> result = em.createQuery(query, TestEntity.class)
		return result;

	public static void main(String[] args) {
		MapPerformanceTest t = new MapPerformanceTest();
		System.err.println("----------- find all -----------");

Henno Vermeulen

-----Oorspronkelijk bericht-----
Van: Rick Curtis [] 
Verzonden: woensdag 16 februari 2011 16:49
Onderwerp: Re: N+1 select problem for Map no matter what I try

Can you try using  @PersistentMap(elementCascade =
CascadeType.PERSIST,fetch=FetchType.EAGER) rather than @ElementCollection? I
don't see the N+1 select problem in my tests when using the persistentmap


On Wed, Feb 16, 2011 at 8:36 AM, Henno Vermeulen <>wrote:

> When I have a Map inside a TestEntity (mapped either with @OneToMany or
> @ElementCollection), OpenJPA 2 always performs N + 1 selects when I do the
> simple query "SELECT z FROM TestEntity z".
> A solution to this would of course be great, but I would also be very happy
> if an OpenJPA developer could quickly try this out and confirm to me whether
> or not this is indeed an issue with OpenJPA or that it is simply expected
> behavior.
> Regards,
> Henno Vermeulen
> (p.s. I have tried many settings and the problem occurs no matter what I
> try. See and my
> unanswered post "preventing N+1 select performance problem on maps").

View raw message