java - delete record by any column value hibernate -


I am new to hibernate, I am learning to be struts2 + hibernate and understand from the example on the net. In one of the examples

what I can see and do, remove the user from their ID, as we can see here, each user in the list has a remove link and the session . Delete () deletes the record by id, now I want the user to enter a name in the text field given there and delete it by that name. So far I've tried this type of

ActionMethod:

  delete public string () {// HttpServletRequest request = (HttpServletRequest) ActionContext .getContext () .get (Serial Action Context.httpiequost); // UsdiododelUser (long.parlong (request.getParameter ("id")); UserDAO.delUser (user.getName ()); Return success; }  

DAOImpl:

  @ Override Public Zero Dealuser (string username) {try user user = (user) session .get (User.class, userName); Session.delete (user); } Hold (exception e) {transaction.rollback (); E.printStackTrace (); }}   

I hope there is a way that hibernation can remove the row by any field value. Nothing above.

The problem is that you can do some property value with hibernate The example of persistence can not be obtained, only by id if you want to find an example by userName

  Public Static Last String USER_NAME = "userName"; Private listing & lt; Users & gt; FindByProperty (string property name, object value) {log.debug ("Find user example with property:" + propertyName + ", value:" + value); Try {string queryString = "From the user model to model." + PropertyName + "=?"; Query queryback = getSession (). CreateQuery (queryString); QueryObject.setParameter (0, value); Return queryObject.list (); } Hold (runtime expansion) {log. Terror ("Search by name property failed", then); Throw away; }} Public listing & lt; Users & gt; FindByUserName (object username) {return findByProperty (USER_NAME, userName); } @ Override Public Wide Delete User (String Username) {{list & lt; Users & gt; List = findByUserName (userName); For (user user: list) getSession (). Delete (user); Log.debug ("successful delete"); } Hold (RuntimeException re) {log. Terror ("Delete Fail", again); Throw away; }}   

which will delete all users by the given name, to delete the unique user, make sure that there is a unique barrier in the user_name area. Can be done through

  @Table (name = "user", uniqueConstraints = {@UniqueConstraint (columnNames = "user_name")})    

Comments

Post a Comment

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

jsp - Google line chart x-axis shrinks on transition -

java - Reaching JTextField in a DocumentListener -