0

我有一个实体类的3个字段,我不希望它是唯一的,而是我希望它们被用作一个必须本身是唯一键的组合字段。 我类POJO:对唯一组合键的验证检查

@Entity 
     @Table(name="EMPLOYEE") 
     public class Employee { 

      @Id 
      @GeneratedValue(strategy = GenerationType.IDENTITY) 
      private int id; 

      @Size(min=3, max=50) 
      @Column(name = "NAME", nullable = false) 
      private String name; 

      @Size(min=3, max=50) 
      @Column(name = "A", nullable = false) 
      private String a; 
      @Size(min=3, max=50) 
      @Column(name = "B", nullable = false) 
      private String b; 
      @Size(min=3, max=50) 
      @Column(name = "C", nullable = false) 
      private String c; 

      @NotNull 
      @DateTimeFormat(pattern="dd/MM/yyyy") 
      @Column(name = "JOINING_DATE", nullable = false) 
      @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate") 
      private LocalDate joiningDate; 

      @NotNull 
      @Digits(integer=8, fraction=2) 
      @Column(name = "SALARY", nullable = false) 
      private BigDecimal salary; 

      @NotEmpty 
      @Column(name = "SSN", unique=true, nullable = false) 
      private String ssn; 
    } 

林表单提交工作,经由JSR303注解验证用户输入验证失败的。在情况下,缺省错误信息是shown.I构成的ResourceBundleMessageSource会

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "com.fussa.fyby") 
public class AppConfig { 

    @Bean 
    public ViewResolver viewResolver() { 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setViewClass(JstlView.class); 
     viewResolver.setPrefix("/WEB-INF/views/"); 
     viewResolver.setSuffix(".jsp"); 
     return viewResolver; 
    } 

    @Bean 
    public MessageSource messageSource() { 
     ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); 
     messageSource.setBasename("messages"); 
     return messageSource; 
    } 
} 

/src/main/resources/messages.properties

Size.employee.name=between {2} and {1} characters long 
NotNull.employee.joiningDate=can not be blank 
NotNull.employee.salary=are u working for free ! 
Digits.employee.salary=Only numeric data with max 8 digits and with max 2 precision is allowed 
NotEmpty.employee.ssn=can not be blank 
typeMismatch=Invalid format 
non.unique.ssn=SSN {0} already exist. 

我发现创建一个独特的密钥此解决方案:

@Table(name = "EMPLOYEE", 
     uniqueConstraints = { @UniqueConstraint(columnNames = { "A", "B", "C" }) }) 

我的问题是我怎么能如果的UniqueConstraint已经违反显示使用messages.properties的消息?

更新1

@Service("employeeService") 
@Transactional 
public class EmployeeServiceImpl implements EmployeeService { 

    @Autowired 
    private EmployeeDao dao; 

    //.... 

    public void saveEmployee(Employee employee) { 
     dao.saveEmployee(employee); 
    } 

} 

感谢您的任何建议..

+0

有当的UniqueConstraint违反多数民众赞成抛出的异常。如果是的话,你可能会抓住这个异常并显示你想要的任何消息。只是一个sugesstion。 –

回答

0

我设法创建一个唯一约束验证了一个字段,如下因素此步骤:

1-创建约束注释。 2-create约束验证器。 3-错误消息。 4-如何使用约束。

我发布的代码与这里的解决方案:Link ..

1

如果你想使用Bean验证你需要写这验证了三列是唯一一个自定义的约束。请注意,这有一个问题。除非你锁定整个表格,否则你总会遇到这样的问题:当你的约束检验器检查唯一性时,另一个并发事务改变数据库。另请参阅此Stackoverflow问题 - Unique constraint with JPA and Bean Validation。 Validator Wiki上有一个blog post关于Unique约束如何看起来像,但它提供了刚才提到的警告。验证可能会通过,但随后在插入期间,由于另一个事务同时修改了数据(因此您的代码也需要处理这种情况),您仍然可能会得到数据库级异常。此外,对于Bean Validation 1.1,您可能直接注入SessionFactory