Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FinderException

 
bksella sella
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a findBy method which takes in 3 parameters and returns a remote interface ( not a collection ). In DB there is exactly one record which matches the findBy methods 3 parameters. In this case i am able to retrieve record from DB. I also have a condition where if i get a FinderException, i create a new record or else i update the existing record. The problem now i am facing is when there are duplicate records in DB, i get a FinderException and so a new record is created in DB, now totally make it three records which match the findBy method condition. I hope my problem is clearly explained. Can anyone pls give me a solution. Thanks in advance.

Karthik
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the 3 parameters you are accepting in your finder do not constitute the primary key of your table, then indicate the same in the finder as well.Your finder should return a collection in this case.If you still choose to return the remote/local interface of the bean then make sure that you check for duplication when you do a create() and not allow for creation of duplicate records in the table.

If i remeber correctly a finder returning a Collection will not throw a finder exception if no records are returned as a result of the query.

So you have to check for the size of the collection before creating a new record in this case
I mean if coll.size() <= 0 then go ahead and create the record in the database
 
bksella sella
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks karthik for your suggestion...
bksella.
 
David McDonagh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik could bksella also use DISTINCT in his EJB QL to only return one record?
 
bksella sella
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
Here is the findBy code which returns multiple rows ( due to duplicated records in DB ).

<finder>
<method-name>findByEmpidTypeAddress</method-name>
<method-params>
<method-param>java.lang.Long</method-param>
<method-param>java.lang.Long</method-param>
<method-param>java.lang.Long</method-param>
</method-params>
<finder-query><![CDATA[(&(&(= empId $0)(= type $1))(= address $2))]]>
</finder-query>
</finder>

Now how do i use the distinct in the above piece of code ( as u suggested )...
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!