Hello Vano,
You can either use flush or run a query that checks for the value in the
database. em.persist() will not flush by itself, and commit happens between your
SLSB exits and the web layer gets back the result (because the commit is done by
the container).
Regards,
-marina
Vano Beridze wrote:
> Hello
>
> I don't know if it's the right place to post but hope to hear some
> answer to my question.
>
> I'm building the simple application using NetBeans 6.
>
> I've created Enterprise Project.
> I've created one entity called product with two fields product_id,
> product_name.
> I indicated that product_name should be unique.
> I've created one stateless session bean with a business method called
> createProduct that accepts productName as a parameter, creates Product
> entity instance and simply calls EntityManager.persist();
>
> I'm calling this method from a web application that uses Visual Java
> Server Faces Framework.
> When I'm creating a product with some name everything works fine and
> updated table is displayed to the user. The problem is that if user
> tries to create a product that already exists, I should notify him about
> this.
> So I created DuplicateEntityException business Exception class and I'm
> throwing it from my business method located in Stateless session bean.
> But in order to throw that exception I should catch another one that's
> thrown after calling em.persist();
> I found out that em.persist() throws exception when I'm creating already
> existing product but that exception is thrown in the Web layer. If I
> call em.flush(); after em.persist() that exception is thrown in my
> stateless session bean method.
>
> The question is: Is it "correct" to call em.flush() to achieve such
> functionality. If it's not "correct" is there any other "correct" way of
> doing this?
>
> Thank you.
>