0 Replies Latest reply: Sep 29, 2008 4:05 AM by gopi b RSS

    our Own Contraints @UserName

    gopi b Newbie

      I wrote my own constraints @username If Username is registered and it is already present in the database , I should dispaly Username Already Exists , I wrote the code , but I am getting the below exception .


      1.Exception during request processing:
      Caused by java.lang.IllegalStateException with message: "Could not commit transaction"

      2.Caused by javax.transaction.RollbackException with message: "[com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] Can't commit because the transaction is in aborted state"

      ------------------
      Code:UserName.java
      --------------------

      package com.manam.mortgage.entity;

      import java.lang.annotation.Documented;
      import static java.lang.annotation.ElementType.FIELD;
      import static java.lang.annotation.ElementType.METHOD;
      import java.lang.annotation.Retention;
      import static java.lang.annotation.RetentionPolicy.RUNTIME;
      import java.lang.annotation.Target;

      import org.hibernate.validator.Validator;
      import org.hibernate.validator.ValidatorClass;
      @Documented
      @ValidatorClass(UserNameValidator.class)
      @Target({METHOD, FIELD})
      @Retention(RUNTIME)
      public @interface UserName {
      String tablename();
      String columnname();
      String message() default "validator.user";

      }

      ----------------------
      UserNaemValidator.java

      ---------------------


      package com.manam.mortgage.entity;

      import java.io.Serializable;
      import java.util.List;


      import org.hibernate.validator.Validator;


      import javax.persistence.EntityManager;
      import javax.persistence.NoResultException;
      import javax.persistence.Query;

      import org.hibernate.validator.PropertyConstraint;
      import org.jboss.seam.Component;
      import org.jboss.seam.annotations.In;

      public class UserNameValidator implements Validator, Serializable {
      private String tablename;
      private String columnname;
      @In EntityManager entityManager;
      private UserName parameters;
      public void initialize(UserName parameters) {
      this.parameters = parameters;
      this.tablename=parameters.tablename();
      this.columnname = parameters.columnname();
      }
      public boolean isValid(Object v){
      if(v==null)
      return true;
      String fieldvalue=(String)v;
      System.out.println("****************************");
      System.out.println("column name:"+columnname);
      System.out.println("field vale:"+fieldvalue);
      System.out.println("table name:"+tablename);
      Query q=((EntityManager)Component.getInstance("entityManager")).createQuery("from " +tablename+ " where " +columnname+ " =:fieldvalue ");
      q.setParameter("fieldvalue",fieldvalue);
      try{
      q.getSingleResult();
      return true;
      }catch(final NoResultException e){
      System.out.println("Not Exists Boss ........");
      return false;
      }
      }
      }



      Please help me any body, Thanking you.