-->
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.  [ 1 post ] 
Author Message
 Post subject: 1-N bidirectional assoc. difference database and 2nd cache.
PostPosted: Mon Sep 15, 2014 10:48 am 
Newbie

Joined: Thu Jul 31, 2014 10:45 am
Posts: 3
Here are parts of two class types,

Code:
public class Address {
    Set<Employee>employees=new HashSet<Employee>();

public class Employee {
    private Address address;;


Employees works on the address, for example. The mapping to Hibernate of them is (partly),

Code:
<class name="Address">
    <cache usage="read-write"/>
    <set name="employees" inverse="false">
         <cache usage="read-write"/>   
             <key column="address_" not-null="true"/>
             <one-to-many class="Employee"/>
    </set>
</class>

<class name="Employee" >
    <cache    usage="read-write"/>
    <many-to-one name="address" column="address_" not-null="true" insert="false" update="false"/>


So we have a bidirectional one-to-many association. The persistence of the association is determined by the collection side , not by the Address references in the employees. To check this I deliberately add Address references to both employees. When both employees are persisted to the database, I see this is indeed the case. But the two Address references are persisted to the second llevel cache, and this is what I do not understand.

Code:
                     
    Employee employee=new Employee(1l,"Foo1",1.00);
    Employee employee2=new Employee(2l,"Foo2",2.00);
    Address address=new Address(12l,"foostreet", "12 foo", "FooCity12");
    Address address3=new Address(34l,"foostreet", "34 foo", "FooCity34");

    address.getEmployees().add(employee);
    address.getEmployees().add(employee2);

    employee.setAddress(address3);                                 
    employee2.setAddress(address3);                         
      
    session.save(address);
    session.save(address3);
    session.save(employee);
    session.save(employee2);
         
    tx.commit();


Then in a new session,

Code:
          
    employee= (Employee) session.get(Employee.class, 1l)l
    address=employee.getAddress();
    employee= (Employee) session.get(Employee.class, 2l);
    address=employee.getAddress();


Without Ehcache address is (a proxy to) Address#12l, so the container of the set where the employees belongs to, Address#12l. But with Ehcache address is aproxy to Address#34l, so the reference to the Address which we set deliberately.

I don't know how this could ever work, because

Code:
BackrefPropertyAccessor$BackrefSetter.set(Object, Object, SessionFactoryImplementor){
}


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

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.