Hi
I've the following mapping, some Category entity:
Code:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@org.hibernate.annotations.BatchSize(size = 30)
public class Category extends AbstractEntity
{
(..)
private static final String CATEGORY_ONETOMANY_INDEX_COLUMN_NAME = "position_in_parent_children";
private static final String CATEGORY_ONETOMANY_JOIN_COLUMN_NAME = "parent_id";
@ManyToOne
@JoinColumn(name = CATEGORY_ONETOMANY_JOIN_COLUMN_NAME, insertable = false, updatable = false)
private Category parent;
@OneToMany
@JoinColumn(name = Category.CATEGORY_ONETOMANY_JOIN_COLUMN_NAME)
@org.hibernate.annotations.IndexColumn(name = Category.CATEGORY_ONETOMANY_INDEX_COLUMN_NAME)
private List<Category> children = new ArrayList<Category>();
@SuppressWarnings("unused")
@Column(name = CATEGORY_ONETOMANY_INDEX_COLUMN_NAME, insertable = false, updatable = false)
private Integer position;
(..)
}
It actually comes from this
topic, which shows how to let hibernate handle the index in an ordered collection (with two unidirectional relationships on the same join column AFAIK). I've just removed the nullable=false on both sides since in my case the parent can indeed be null.
When I retrieve the parent from some instance, it works fine only if I disable the second level cache like that :
Code:
properties.put("hibernate.cache.use_second_level_cache", "false");
However, for sure, we need caching to be done.
I tried to specify some @Cache usage like NONE or READ_WRITE on the parent member itself but it doesn't change anything. Making the fetch eager didn't help neither.
Any suggestion ?
FYI, unit test code + versions used:
Code:
public void testFindById() throws Exception
{
Category parent = new Category();
Category child1 = new Category();
Injector injector = getInjector();
CategoryDao categoryDao = injector.getInstance(Category.CategoryDao.class);
parent.getChildren().add(child1);
categoryDao.getEntityAccessAspect().save(child1);
categoryDao.getEntityAccessAspect().save(parent);
changeEntityManagerProvider();
Category found = emp.get().find(Category.class, child1.getId());
assertNotNull(found);
assertNotNull(found.getParent()); // FAILS
}
Code:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.0.CR1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.CR1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.3.0.CR2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.3.0.CR1</version>
</dependency>
<dependency>
<groupId>cglib-nodep</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
</dependencies>
Eager to read you !