Results 1 to 7 of 7
  1. #1
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default Jpa2 entity update problem

    Hi All, i develop a app usign spring3 jpa2 hibernate3.x and struts2. I have defined a set of entities for my db tables. I have read about jpa relationships and how to define them, (oneToOne, manyToMany...) and applied this to my app.The problem is that when i update a entity the change is not appwide propagated.
    so, i have the following entities :
    User
    Company
    Location
    A user is assigned to one or more locations and a company has one or more locations also. So via location i can establish a relation between user and company.

    Java Code:
    @Entity
    @Access(AccessType.PROPERTY)
    @Table(name="users")
    public class User implements Serializable {
    
    //properties, setters and getters...
    
     @ManyToMany
        @JoinTable(name="locations_users",
                    joinColumns = { @JoinColumn(name="user_id")},
                    inverseJoinColumns = {@JoinColumn(name="location_id")}
                    )
        public List<Location> getLocations(){
            return this.locations;
        }
    
        public void addLocation(Location l){
            if (!this.locations.contains(l))
                  if (!l.getUsers().contains(this))
                    l.addUser(this);
                
                this.locations.add(l);
            }
    
        public boolean removeLocation(Location l){        
        	if (this.locations.contains(l)){
                if(l.getUsers().contains(this))
                    l.removeUser(this);
                this.locations.remove(l);
            }
    }
    as you can see here when i acess addLocation() or removeLocation() i take care to update the location entity also.
    The problem is that after i add/remove a location from a user if i go to the page that list users locations(this time coming from Company->getLocations()->getUser()) the change is not there. I can fix this if i run this.entityManager.refresh() before getting a location for a user. but this is not the behavior that i was expected. As far as i debugged i guess that this location object (entity) has 2 or more instances, and when i update one the other remains obsolete. how do i have to handle this situation ?

    PS: my EntityManager is defined as a:
    Java Code:
    @PersistenceContext(type = PersistenceContextType.EXTENDED,unitName = "PUName")
        protected EntityManager em;
    and in applicationCOntext.xml i have defined :
    Java Code:
    <bean id="txManagerVA"  class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory"  ref="emfVA" />
     </bean>
    
    <bean id="emfVA" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="vsDS" />  
      <property name="persistenceUnitName" value="PUName"/>
      <property name="persistenceXmlLocation" value="META-INF/persistence.xml" /> 
    </bean>
    Thanks,
    Adrian

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Why are there multiple instances?
    Sounds to me like your design is a bit on the odd side here.

  3. #3
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default

    i do not know why this behaves like there were multiple instances. Why is my design Odd ?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Because there probably shouldn't be multiple instances, other than for serving multiple users. In which case I wouldn't expect one to magically update if another one has been updated somewhere else.

    Essentially, there is entity A and entity B. Both point to the same data in the database. Each is in its own transaction. B is changed. A still refers to the old data, because that's how transactions work. In isolation.

    Now, if you are expecting A to be updated then there is a problem with your design. IMO.

  5. #5
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default

    i got your point, it might be very well a design problem because i`m new to jpa. But my understanding is that if as you said i have two entities A and B which refer the same row in one table, and from entity B i update something with persist() and flush() the A would be automatically updated. Or do i have to use refresh() before use ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I suspect you'd need to refresh it, otherwise how would it know something behind it had changed? Automatically changing something would break transactional isolation, which is quite important.

    I still don't see how you have two of these active at the same time. Are you holding onto them somewhere?
    Because I would expect someone going to a page that will display data from that row to get that row fresh from Hibernate/JPA...which would get the updated row.

  7. #7
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default

    First of all thanks for the quick replies.
    I think that i have to read more about spring transactions and to have a better understanding of these concepts. I`ll come back when have all this clear.
    Thanks again.

Similar Threads

  1. Replies: 0
    Last Post: 04-22-2011, 09:22 PM
  2. DB Update Problem
    By anjibman in forum JDBC
    Replies: 14
    Last Post: 01-24-2011, 03:37 PM
  3. Hibernate, JPA2, Spring???
    By lat3ncy in forum New To Java
    Replies: 1
    Last Post: 03-04-2010, 04:45 AM
  4. SQL update problem
    By skiing in forum New To Java
    Replies: 9
    Last Post: 11-21-2008, 07:54 PM
  5. problem with ejb 3.0 entity beans with manyToMany relationship
    By makcro in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 07-26-2007, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •