我正在尝试实现自定义注释,因为它是在post中编写的,但出了点问题。当我试图在dataBase中保存我的实体时,它会使用我的自定义注释进行无限检查。我正在使用wildfly 9.0.2
代码:
实体:
@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...
}
注释:
@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 {};
}
验证
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;
}
}
链接到server.log(snip2code)
提前谢谢!
答案 0 :(得分:1)
所以问题是,当我从验证器调用userExist(String username)
时,它需要父事务以及@maress如何说,转到无限循环。我通过在userExist
方法禁用事务来解决此问题。所以现在使用此更改代码将起作用
@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;
}