Results 1 to 7 of 7
Thread: Jpa2 entity update problem
- 06-08-2011, 09:14 AM #1
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
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.
as you can see here when i acess addLocation() or removeLocation() i take care to update the location entity also.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); } }
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:
and in applicationCOntext.xml i have defined :Java Code:@PersistenceContext(type = PersistenceContextType.EXTENDED,unitName = "PUName") protected EntityManager em;
Thanks,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>
Adrian
- 06-08-2011, 09:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Why are there multiple instances?
Sounds to me like your design is a bit on the odd side here.
- 06-08-2011, 10:00 AM #3
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
i do not know why this behaves like there were multiple instances. Why is my design Odd ?
- 06-08-2011, 10:09 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 06-08-2011, 10:14 AM #5
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
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 ?
- 06-08-2011, 10:28 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 06-08-2011, 11:05 AM #7
Member
- Join Date
- Dec 2010
- Location
- Bucharest Romania
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
JPA/JPA2: please tell me if I understand this correctly.
By kjkrum in forum JDBCReplies: 0Last Post: 04-22-2011, 08:22 PM -
DB Update Problem
By anjibman in forum JDBCReplies: 14Last Post: 01-24-2011, 02:37 PM -
Hibernate, JPA2, Spring???
By lat3ncy in forum New To JavaReplies: 1Last Post: 03-04-2010, 03:45 AM -
SQL update problem
By skiing in forum New To JavaReplies: 9Last Post: 11-21-2008, 06:54 PM -
problem with ejb 3.0 entity beans with manyToMany relationship
By makcro in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 07-26-2007, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks