Моя аннотация @Positive по-прежнему принимает отрицательное значение.
Код, проверяющий поле:
@Basic(optional = false)
@NotNull
@Positive(message = "value must be positive and 2 digits")
@Digits(integer=2, fraction=0, message = "value must be positive and 2 digits whole number")
@Column(name = "add_user")
private int addUser;
Я использую импорт
import javax.validation.constraints.Positive;
, зависимости, которые я использую,
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.1.2.Final</version>
</dependency>
поле формы для отображения ошибки:
<tr>
<td><form:label path="addUser">Add User</form:label></td>
<td><form:input path="addUser"/></td>
<td style="color:red"> <form:errors path="addUser"/> </td>
</tr>
метод контроллера:
@PostMapping("/addBrewery")
public ModelAndView addAnBrewery(ModelMap model, @Valid @ModelAttribute("brewerie") Breweries brewerie, BindingResult result) {
if (result.hasErrors()) {
System.out.println(result);
return new ModelAndView("/addBrewery");
}
service.addAnBreweries(brewerie);
return new ModelAndView("redirect:/breweries");
}
Класс объектов Breweries представляет собой JPA-код для Breweries. java является следующим, I должны удалить геттеры и сеттеры из-за количества кода и отсутствия текста в этом вопросе
@Entity
@Table(name = "breweries")
@SecondaryTable(name = "breweries_geocode", pkJoinColumns = @PrimaryKeyJoinColumn(name = "brewery_id", referencedColumnName = "id"))
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Breweries.findAll", query = "SELECT b FROM Breweries b"),
@NamedQuery(name = "Breweries.findById", query = "SELECT b FROM Breweries b WHERE b.id = :id"),
@NamedQuery(name = "Breweries.findByName", query = "SELECT b FROM Breweries b WHERE b.name = :name"),
@NamedQuery(name = "Breweries.findByAddress1", query = "SELECT b FROM Breweries b WHERE b.address1 = :address1"),
@NamedQuery(name = "Breweries.findByAddress2", query = "SELECT b FROM Breweries b WHERE b.address2 = :address2"),
@NamedQuery(name = "Breweries.findByCity", query = "SELECT b FROM Breweries b WHERE b.city = :city"),
@NamedQuery(name = "Breweries.findByState", query = "SELECT b FROM Breweries b WHERE b.state = :state"),
@NamedQuery(name = "Breweries.findByCode", query = "SELECT b FROM Breweries b WHERE b.code = :code"),
@NamedQuery(name = "Breweries.findByCountry", query = "SELECT b FROM Breweries b WHERE b.country = :country"),
@NamedQuery(name = "Breweries.findByPhone", query = "SELECT b FROM Breweries b WHERE b.phone = :phone"),
@NamedQuery(name = "Breweries.findByWebsite", query = "SELECT b FROM Breweries b WHERE b.website = :website"),
@NamedQuery(name = "Breweries.findByImage", query = "SELECT b FROM Breweries b WHERE b.image = :image"),
@NamedQuery(name = "Breweries.findByAddUser", query = "SELECT b FROM Breweries b WHERE b.addUser = :addUser"),
@NamedQuery(name = "Breweries.findByLastMod", query = "SELECT b FROM Breweries b WHERE b.lastMod = :lastMod"),
@NamedQuery(name = "Breweries.findByCreditLimit", query = "SELECT b FROM Breweries b WHERE b.creditLimit = :creditLimit"),
@NamedQuery(name = "Breweries.findByEmail", query = "SELECT b FROM Breweries b WHERE b.email = :email")})
public class Breweries implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
@Positive(message = "value must be positive")
private Integer id;
@Basic(optional = false)
@NotNull
@Column(name = "name")
@NotBlank(message = " field cannot be empty")
@Size(min = 1, max = 50, message = "must be 50 characters or less")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50, message = "must be 50 characters or less")
@Column(name = "address1")
private String address1;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50, message = "must be 50 characters or less")
@Column(name = "address2")
private String address2;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30, message = "must be 30 characters or less")
@Column(name = "city")
private String city;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20, message = "must be 20 characters or less")
@Column(name = "state")
private String state;
@Basic(optional = false)
@NotNull
//@Size(min = 1, max = 5, message = "must be 5 digits or less")
@NotBlank(message = " field cannot be empty")
@Pattern(regexp="\\d{1,5}", message = "must be 5 digits or less")
//@digit
@Column(name = "code")
private String code;
@Basic(optional = false)
@NotNull()
@Column(name = "country")
@NotBlank(message = " field cannot be empty")
private String country;
// @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
@Basic(optional = false)
@NotNull()
@NotBlank(message = " field cannot be empty")
@Column(name = "phone")
private String phone;
@Basic(optional = false)
@NotNull
@URL( message = "must be a valid url")
@NotBlank(message = " this field cannot be blank")
@Column(name = "website")
private String website;
@Basic(optional = false)
// @NotNull
@Size(min = 1, max = 255)
@Column(name = "image")
private String image;
@Basic(optional = false)
@NotNull
@Lob
@NotBlank(message = " this field cannot be blank")
@Size(min = 1, max = 65535)
@Column(name = "description")
private String description;
@Basic(optional = false)
@NotNull
@Positive(message = "value must be positive and 2 digits")
// @Digits(integer=2, fraction=0, message = "value must be positive and 2 digits whole number")
@Column(name = "add_user")
private int addUser;
@Basic(optional = false)
// @NotNull
@Past()
@Column(name = "last_mod")
@Temporal(TemporalType.TIMESTAMP)
private Date lastMod;
@Basic(optional = false)
@NotNull
@Digits(integer = 6, fraction = 2, message = "Must have a max of 6 digits before the decimal point and 2 afterwards.")
@Column(name = "credit_limit")
private double creditLimit;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Basic(optional = false)
@NotNull
//@Size(min = 1, max = 50)
@NotBlank(message = " this field cannot be blank")
@Column(name = "email")
@Email(message= " this is not a valid email adress")
private String email;
@Column(table = "breweries_geocode")
@NotNull
//@Size(min = -90, max = 90, message = "latitide must be between -90 and 90")
@Max(value = 90, message = "latitide must be between -90 and 90")
@Min(value = -90, message = "latitide must be between -90 and 90")
private float latitude;
@NotNull
@Column(table = "breweries_geocode")
// @Size(min = -180, max = 180, message = "latitide must be between -180 and 180")
@Max(value = 80, message = "latitide must be between -180 and 80")
@Min(value = -180, message = "latitide must be between -180 and 80")
private float longitude;
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Breweries)) {
return false;
}
Breweries other = (Breweries) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "lit.sd4.model.Breweries[ id=" + id + " ]";
}
}