简体   繁体   中英

infinite check for custom JSR303 annotation

I'm trying to implement custom annotation as it is written in this post , but something goes wrong. When I'm trying to persist my entity in dataBase, it gonna infinite check field with my custom annotation. I'm using wildfly 9.0.2

code:

Entity:

@Entity
@Table(name = "users")
@NamedQueries({
        @NamedQuery(name = User.FIND_BY_LOGIN_AND_USERNAME,query = "SELECT u FROM User u WHERE u.userName = :username and u.password = :password"),
        @NamedQuery(name = User.FIND_USER_BY_USERNAME,query = "SELECT u FROM User u WHERE u.userName = :username")
})
public class User implements Serializable{

    public static final String FIND_BY_LOGIN_AND_USERNAME = "findByLoginAndPassword";
    public static final String FIND_USER_BY_USERNAME = "findUserByUsername";

    @Id
    @GeneratedValue
    private Long id;
    @NotNull
    @Size(min = 3,max = 64,message = "{model.User.login.message}")
    @Username
    @Column(name = "user_name",nullable = false,unique = true)
    private String userName;

    //...other fields...

    //...constructors, getters and setters...
}

Annotation:

@Constraint(validatedBy = UsernameValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.ANNOTATION_TYPE})
public @interface Username {
    String message() default "Username already exists";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

Validator:

public class UsernameValidator implements ConstraintValidator<Username, String> {

    @Inject
    UserManager userManager;

    @Override
    public void initialize(Username constraintAnnotation) {

    }

    @Override
    public boolean isValid(String username, ConstraintValidatorContext context) {
        boolean isValid = !userManager.userExist(username);
        return isValid;
    }
}

UserManager:

@Stateless
public class UserManager {

    @PersistenceContext(unitName = "MySqlPU")
    EntityManager em;

    public void persist(User user){
        em.persist(user);
    }

    public User findUserByLoginAndPassword(String username, String password) {
        try {
            return em.createNamedQuery(User.FIND_BY_LOGIN_AND_USERNAME, User.class)
                    .setParameter("username",username)
                    .setParameter("password",password)
                    .getSingleResult();
        } catch (NoResultException e){
            return null;
        }
    }

    public boolean userExist(String username) {
        try {
            em.createNamedQuery(User.FIND_USER_BY_USERNAME, User.class)
                    .setParameter("username", username)
                    .getSingleResult();
        } catch (NoResultException e) {
            return false;
        }
        return true;
    }
}

link to server.log (snip2code)

Thank you in advance!

So the problem is, when i call userExist(String username) from my validator, it takes parent transaction and how @maress says, go to infinite loop. I fixed this problem by disableing transaction at userExist method. So now with this changes code will be work

    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public boolean userExist(String username) {
        try {
            em.createNamedQuery(User.FIND_USER_BY_USERNAME, User.class)
                    .setParameter("username", username)
                    .getSingleResult();
        } catch (NoResultException e) {
            return false;
        }
        return true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM