These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: @ManyToOne field null when 2nd level cache enabled
PostPosted: Thu Apr 08, 2010 4:48 am 
Newbie

Joined: Sun Apr 02, 2006 4:33 pm
Posts: 7
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 !


Top
 Profile  
 
 Post subject: Re: @ManyToOne field null when 2nd level cache enabled
PostPosted: Thu Apr 08, 2010 6:00 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:
parent.getChildren().add(child1);


Maybe you should also call

Code:
child1.setParent(parent);


Top
 Profile  
 
 Post subject: Re: @ManyToOne field null when 2nd level cache enabled
PostPosted: Thu Apr 08, 2010 8:08 am 
Newbie

Joined: Sun Apr 02, 2006 4:33 pm
Posts: 7
unfortunately, it didn't help, at least directly.

Indeed, I then tried with categories being already persisted and it did work... still I don't know how to resolve it when I'm creating these entities.


Top
 Profile  
 
 Post subject: Re: @ManyToOne field null when 2nd level cache enabled
PostPosted: Thu Apr 08, 2010 8:27 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you to try it with hibernate 3.5,
it the behavior is the same in hibernate 3.5 , then in my opinion it is a bug which should be reported.


Top
 Profile  
 
 Post subject: Re: @ManyToOne field null when 2nd level cache enabled
PostPosted: Thu Apr 08, 2010 8:46 am 
Newbie

Joined: Sun Apr 02, 2006 4:33 pm
Posts: 7
I just spotted that the same mapping pattern but on 2 distinct entities work fine...

I guess it makes it even closer from a bug... I would have preferred an error on my side!

EDIT: I'm on my way to test with 3.5, I'll let you know :)


Top
 Profile  
 
 Post subject: Re: @ManyToOne field null when 2nd level cache enabled
PostPosted: Thu Apr 08, 2010 3:20 pm 
Newbie

Joined: Sun Apr 02, 2006 4:33 pm
Posts: 7
hi

it didn't work neither with 3.5

I will wrap a few junit tests to show the different issue (before/after 3.5) and then create a jira for it.

thanks again for your help


Top
 Profile  
 
 Post subject: Re: @ManyToOne field null when 2nd level cache enabled
PostPosted: Mon Apr 12, 2010 4:58 am 
Newbie

Joined: Sun Apr 02, 2006 4:33 pm
Posts: 7
Ok, trouble identified: it's a bug which was reported in January 2007 and which is still waiting for a patch, cf 2nd level cache broken for non-inverse bidirectional one-to-many relation.

Quite a pity... It breaks heavily this pojo pattern which hibernate should be all about, since now we'll have to somehow managed the order independently from the list...

Anyway, thanks for your help pb00067.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.