• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Multiple representations of the same entity are are being merged  RSS feed

 
Ranch Hand
Posts: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the following error


I know what is causing the problem, but not how to fix it.

i have the following manyToMany mapping

Bank <-> Account <-> Fund

I have a Bank BankId 123 that bank has Account accountId 1234 with fund fundId 12345
If I add to Bank  BankId 123  a new Account accountId 1235 but with with fund fundId 12345
It will fall over

It wont allow me to add an existing fund, from one account to another account, if both accouts are mapped to the same Bank.  I am using org.springframework.data.jpa.repository.JpaRepository; save() for my entity

The Entities are






Thanks for any help in a work around.
 
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please keep example code to a minimum. While we do need to see all the details of what you're doing, the stuff that's unrelated to what you're having problems with just gets in the way. Besides, there's always the possibility you might discover what the problem is as part of the paring down process.

Anyway, I wasn't sure if this a a many-to-many issue or not, since I didn't read the code line-by-line, but my first suspicion has to to with attachments.

In JPA, an Entity can either be attached (connected to the database) or detached (a free-floating POJO). My own personal preference is to detach everything in my data services layer, have the business logic do its thing, then the business logic returns the updated data (working set) to the data services, which re-attaches to the database and persists the data to the database server).

There are some "gotchas" here, though. Attaching a detached object isn't simply taking the object and hooking a connection to it. Instead the attachment process creates a whole new instance of the object and its relatives.

The item(s) returned from the attach process obey the "equals()" property of the original objects, but not the "==" property. This is even more confusing when you consider that creating an entity object initially and saving it may return the same object ("==") that you saved.

So you have to tread carefully, and immediately stop using the detached items once you've re-attached. AND that includes allowing for lazy-fetch relations!
 
Tony Evans
Ranch Hand
Posts: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is a detached problem. Lets say I create a object called Bank with a many to many mapping to an acount, and account has a many to many mapping to a fund.

Then I want to update the bank object by adding  another account but maps to the same fund, that will cause the error.  
What I can do is map the account to the bank, and then in a seperate call to the repository, map the fund to the account, and that way it works.

 
Tim Holloway
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do a 2-tier approach to complex persistency. The upper tier (service tier) is the one that business logic invokes. Each method is wrapped in a database transaction (because it's Spring, it's a @Transactional @Repository). So the first thing that the service method has to do is replace all the detached objects with their attached counterparts (merge) and discard the detached ones, since they're no longer valid - if I want to return a working set from the service, it will be a new working set of detached objects.

A brief comment. I use the term "working set" to indicate the record(s) that will be acted on by the business logic while detached. Typically there will be an anchor record, like for example, a bus route, and related records (route stops, and so forth). Since this sort of thing often interlinks to the point where you could potentially drag the entire database into memory, I use lazy fetches to limit the actual in-memory objects. But note that a lazy fetch cannot be resolved once an entity is detached, so I have to pre-fetch anything I'll be working with or on before I detach (hence the term "working set").

To make things a little more manageable I have an under-tier, which is the DAO tier. The DAOs contain the low-level CRUD and finder routines, generally one DAO per table, although sometimes, if there's a very tight parent-child relationship, linked tables. The DAO methods are also transactional repository methods, although they're inheriting the transactional environment from the service tier anyway.

By working this way, I can maintain the entities in a neat and consistent manner. And since the entities that the service layer returns are always detached, I never have to worry about the database being accidentally corrupted in mysterious ways, and I can maintain the working set in webapp session data storage, which is something you cannot do safely using attached objects.
 
Tony Evans
Ranch Hand
Posts: 632
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is roughly what I have done. I follow MVC. I have a control layer, a service layer and a repository layer, extending JPARepository. I dont get this problem using Hibernate session factory directly. I guess its one of those things, i will have to play around with, to get working.

Thanks for the reply. Its always good to discuss design, which si just as importannt as coding/
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!