Exception JSF Delete from Database [duplicate]












1















This question already has an answer here:




  • catch DB exception in JSF+EJB application

    2 answers



  • Letting the presentation layer (JSF) handle business exceptions from service layer (EJB)

    1 answer




I have a problem with my exception handling in JSF.
I want to show an error message on my JSF page when the user can not delete an entry in the database.






<h:message for="deleteButton"/>
<h:commandButton id="deleteButton" value="Delete"
action="#{filmView.deleteRow(film)}" />





public void deleteRow(Film deleteTemp) {

filmService.delete(deleteTemp.getFilmId());

}

public void delete(int id) {

try {
em.createQuery("delete from Film where film_id=" + id).executeUpdate();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage("", new FacesMessage(
"Removing dataset(ID:" + id + ") is not possible, because it's used in another dataset!"));
}
}


So is it possible to get this error message into the JSF page and if yes is this better, because I heard that how I do it right now, it is not a good solution.



Thanks for your help!
Felix










share|improve this question













marked as duplicate by f1sh, BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Why is not a good solution? Does it work or not? Try to identify your problem: is it jpa related or jsf? Can you catch the exception? Can you show a Faces Message?
    – perissf
    Nov 12 '18 at 16:56










  • @perissf It is working, but my problem is that i went from the bottom layer directly to the view and not go from one layer to another and I thought it would be possible to do the exception handling directly on the view.
    – Felix
    Nov 12 '18 at 17:11


















1















This question already has an answer here:




  • catch DB exception in JSF+EJB application

    2 answers



  • Letting the presentation layer (JSF) handle business exceptions from service layer (EJB)

    1 answer




I have a problem with my exception handling in JSF.
I want to show an error message on my JSF page when the user can not delete an entry in the database.






<h:message for="deleteButton"/>
<h:commandButton id="deleteButton" value="Delete"
action="#{filmView.deleteRow(film)}" />





public void deleteRow(Film deleteTemp) {

filmService.delete(deleteTemp.getFilmId());

}

public void delete(int id) {

try {
em.createQuery("delete from Film where film_id=" + id).executeUpdate();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage("", new FacesMessage(
"Removing dataset(ID:" + id + ") is not possible, because it's used in another dataset!"));
}
}


So is it possible to get this error message into the JSF page and if yes is this better, because I heard that how I do it right now, it is not a good solution.



Thanks for your help!
Felix










share|improve this question













marked as duplicate by f1sh, BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Why is not a good solution? Does it work or not? Try to identify your problem: is it jpa related or jsf? Can you catch the exception? Can you show a Faces Message?
    – perissf
    Nov 12 '18 at 16:56










  • @perissf It is working, but my problem is that i went from the bottom layer directly to the view and not go from one layer to another and I thought it would be possible to do the exception handling directly on the view.
    – Felix
    Nov 12 '18 at 17:11
















1












1








1








This question already has an answer here:




  • catch DB exception in JSF+EJB application

    2 answers



  • Letting the presentation layer (JSF) handle business exceptions from service layer (EJB)

    1 answer




I have a problem with my exception handling in JSF.
I want to show an error message on my JSF page when the user can not delete an entry in the database.






<h:message for="deleteButton"/>
<h:commandButton id="deleteButton" value="Delete"
action="#{filmView.deleteRow(film)}" />





public void deleteRow(Film deleteTemp) {

filmService.delete(deleteTemp.getFilmId());

}

public void delete(int id) {

try {
em.createQuery("delete from Film where film_id=" + id).executeUpdate();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage("", new FacesMessage(
"Removing dataset(ID:" + id + ") is not possible, because it's used in another dataset!"));
}
}


So is it possible to get this error message into the JSF page and if yes is this better, because I heard that how I do it right now, it is not a good solution.



Thanks for your help!
Felix










share|improve this question














This question already has an answer here:




  • catch DB exception in JSF+EJB application

    2 answers



  • Letting the presentation layer (JSF) handle business exceptions from service layer (EJB)

    1 answer




I have a problem with my exception handling in JSF.
I want to show an error message on my JSF page when the user can not delete an entry in the database.






<h:message for="deleteButton"/>
<h:commandButton id="deleteButton" value="Delete"
action="#{filmView.deleteRow(film)}" />





public void deleteRow(Film deleteTemp) {

filmService.delete(deleteTemp.getFilmId());

}

public void delete(int id) {

try {
em.createQuery("delete from Film where film_id=" + id).executeUpdate();
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage("", new FacesMessage(
"Removing dataset(ID:" + id + ") is not possible, because it's used in another dataset!"));
}
}


So is it possible to get this error message into the JSF page and if yes is this better, because I heard that how I do it right now, it is not a good solution.



Thanks for your help!
Felix





This question already has an answer here:




  • catch DB exception in JSF+EJB application

    2 answers



  • Letting the presentation layer (JSF) handle business exceptions from service layer (EJB)

    1 answer







<h:message for="deleteButton"/>
<h:commandButton id="deleteButton" value="Delete"
action="#{filmView.deleteRow(film)}" />





<h:message for="deleteButton"/>
<h:commandButton id="deleteButton" value="Delete"
action="#{filmView.deleteRow(film)}" />






java jpa jsf java-ee






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 16:46









Felix

267




267




marked as duplicate by f1sh, BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by f1sh, BalusC java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Why is not a good solution? Does it work or not? Try to identify your problem: is it jpa related or jsf? Can you catch the exception? Can you show a Faces Message?
    – perissf
    Nov 12 '18 at 16:56










  • @perissf It is working, but my problem is that i went from the bottom layer directly to the view and not go from one layer to another and I thought it would be possible to do the exception handling directly on the view.
    – Felix
    Nov 12 '18 at 17:11
















  • 1




    Why is not a good solution? Does it work or not? Try to identify your problem: is it jpa related or jsf? Can you catch the exception? Can you show a Faces Message?
    – perissf
    Nov 12 '18 at 16:56










  • @perissf It is working, but my problem is that i went from the bottom layer directly to the view and not go from one layer to another and I thought it would be possible to do the exception handling directly on the view.
    – Felix
    Nov 12 '18 at 17:11










1




1




Why is not a good solution? Does it work or not? Try to identify your problem: is it jpa related or jsf? Can you catch the exception? Can you show a Faces Message?
– perissf
Nov 12 '18 at 16:56




Why is not a good solution? Does it work or not? Try to identify your problem: is it jpa related or jsf? Can you catch the exception? Can you show a Faces Message?
– perissf
Nov 12 '18 at 16:56












@perissf It is working, but my problem is that i went from the bottom layer directly to the view and not go from one layer to another and I thought it would be possible to do the exception handling directly on the view.
– Felix
Nov 12 '18 at 17:11






@perissf It is working, but my problem is that i went from the bottom layer directly to the view and not go from one layer to another and I thought it would be possible to do the exception handling directly on the view.
– Felix
Nov 12 '18 at 17:11














1 Answer
1






active

oldest

votes


















1














Ideally your ManagedBean should handle screen messages.




  1. The best way is throw the error from DAO to your Service layer.


  2. On Service layer you can throw again to your ManagedBean and show the message.





On your FilmDAO.java:



public void delete(int id) throws Exception {
em.createQuery("delete from Film where film_id=" + id).executeUpdate();
}


On your FilmService.java:



public void delete(int id) throws Exception {
filmDAO.delete(id);
}


On your FilmMB.java:



try {
filmService.delete(id);
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage("",
new FacesMessage("Removing dataset(ID:" + id + ") is not possible,
because it's used in another dataset!"));
}





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Ideally your ManagedBean should handle screen messages.




    1. The best way is throw the error from DAO to your Service layer.


    2. On Service layer you can throw again to your ManagedBean and show the message.





    On your FilmDAO.java:



    public void delete(int id) throws Exception {
    em.createQuery("delete from Film where film_id=" + id).executeUpdate();
    }


    On your FilmService.java:



    public void delete(int id) throws Exception {
    filmDAO.delete(id);
    }


    On your FilmMB.java:



    try {
    filmService.delete(id);
    } catch (Exception e) {
    FacesContext.getCurrentInstance().addMessage("",
    new FacesMessage("Removing dataset(ID:" + id + ") is not possible,
    because it's used in another dataset!"));
    }





    share|improve this answer


























      1














      Ideally your ManagedBean should handle screen messages.




      1. The best way is throw the error from DAO to your Service layer.


      2. On Service layer you can throw again to your ManagedBean and show the message.





      On your FilmDAO.java:



      public void delete(int id) throws Exception {
      em.createQuery("delete from Film where film_id=" + id).executeUpdate();
      }


      On your FilmService.java:



      public void delete(int id) throws Exception {
      filmDAO.delete(id);
      }


      On your FilmMB.java:



      try {
      filmService.delete(id);
      } catch (Exception e) {
      FacesContext.getCurrentInstance().addMessage("",
      new FacesMessage("Removing dataset(ID:" + id + ") is not possible,
      because it's used in another dataset!"));
      }





      share|improve this answer
























        1












        1








        1






        Ideally your ManagedBean should handle screen messages.




        1. The best way is throw the error from DAO to your Service layer.


        2. On Service layer you can throw again to your ManagedBean and show the message.





        On your FilmDAO.java:



        public void delete(int id) throws Exception {
        em.createQuery("delete from Film where film_id=" + id).executeUpdate();
        }


        On your FilmService.java:



        public void delete(int id) throws Exception {
        filmDAO.delete(id);
        }


        On your FilmMB.java:



        try {
        filmService.delete(id);
        } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage("",
        new FacesMessage("Removing dataset(ID:" + id + ") is not possible,
        because it's used in another dataset!"));
        }





        share|improve this answer












        Ideally your ManagedBean should handle screen messages.




        1. The best way is throw the error from DAO to your Service layer.


        2. On Service layer you can throw again to your ManagedBean and show the message.





        On your FilmDAO.java:



        public void delete(int id) throws Exception {
        em.createQuery("delete from Film where film_id=" + id).executeUpdate();
        }


        On your FilmService.java:



        public void delete(int id) throws Exception {
        filmDAO.delete(id);
        }


        On your FilmMB.java:



        try {
        filmService.delete(id);
        } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage("",
        new FacesMessage("Removing dataset(ID:" + id + ") is not possible,
        because it's used in another dataset!"));
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 17:04









        ℛɑƒæĿ

        1,01621125




        1,01621125















            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()