gpt4 book ai didi

hibernate - 为 hibernate 中的唯一约束添加自定义消息

转载 作者:行者123 更新时间:2023-12-02 04:13:48 30 4
gpt4 key购买 nike

@Entity @Table(name="users",uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})
public class User implements Serializable {
@Column(unique = true,name = "email")
@NotNull(message = "{NotNull.email}")
@Email(message = "{Pattern.email}")
@Size(min=10, max=50)
private String email;

public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}

我有工作正常的 message.properties 文件

 NotNull.email = Email is required

Pattern.email = Enter a valid email address

我可以在 message.properties 文件中添加另一个字段,例如

 Unique.email = Email already exits 

并将此 Unique.email 发送给

@Column(unique = true,name = "email",message="{Unique.email}")

我知道,我们不能在上面的行中保留 message="{Unique.email}"。

但是我们如何向上面的列提供“Unique.email”以获得自定义错误消息

??

提前致谢

最佳答案

模型User.java中用户定义的注释“@Unique”

@Column(unique = true,name = "email")
@Unique(message = "{Unique.email}")
private String email;

消息.属性

Unique.email = Email id already exists

Unique.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Target({ElementType.METHOD, ElementType.FIELD})
@Constraint(validatedBy = UserUniqueValidator.class)
@Retention(RUNTIME)
public @interface Unique {
String message();
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}

UserUniqueValidator.java

import com.User;
import com.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class UserUniqueValidator implements ConstraintValidator<Unique,String> {

@Autowired
private UserService userService;

@Override
public void initialize(Unique unique) {
unique.message();
}

@Override
public boolean isValid(String email, ConstraintValidatorContext context) {
if (userService != null && userService.existsByEmail(email,User.class)) {
return false;
}
return true;
}
}

UserService.java

public interface UserService {
public boolean existsByEmail(String email,Class clazz);
}

UserServiceImpl.java

import com.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl extends UserRepository implements UserService {

@Override
public boolean existsByEmail(String email, Class clazz) {
return super.existsByEmail(email,clazz);
}

}

UserRepository.java

public class UserRepository {
public boolean existsByEmail(String email, Class className) {
Criteria criteria = getSession().createCriteria(className,"userEmail");
criteria.add(Restrictions.eq("email",email));
if(criteria.list().size() == 0){
return false;
}
return true;
}
}

关于hibernate - 为 hibernate 中的唯一约束添加自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50466545/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com