我低于错误
这是我的代码
----------------------- Junit -----------
//在部署程序中完成DeploymentConfigurationMappingPK初始化
@Test
public void test1ConnectionIdIsRequired()
{
connectionMappingPK.setConnectionId(null);
List<String> errorMessages=null;
errorMessages=adminValidator.constraintViolationsForDeploymentConfigMapping(connectionMappingPK);
Assert.assertTrue(errorMessages.contains("ConnectionId must be specified."));
}
DeploymentConfigurationMappingPK.java
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.demo.DBConstants;
@Embeddable
public class DeploymentConfigurationMappingPK implements Serializable {
private static final long serialVersionUID = 1L;
@JoinColumn(name = DBConstants.CONNECTION_ID, nullable = false)
@Column(name = DBConstants.CONNECTION_ID, nullable = false, length = 255)
@NotNull(message="ConnectionId must be specified.")
@Size(min=1, max=255)
private String connectionId;
@Basic
@Column(name = DBConstants.APPLICATION_ID, length = 255)
@NotNull(message="ApplicationId must be specified.")
@Size(min=1, max=255)
private String applicationId;
@Basic
@Column(name = DBConstants.APPLICATION_VERSION, length = 10)
@NotNull(message="ApplicationVersion must be specified.")
@Size(min=1, max=10)
private String applicationVersion;
public String getConnectionId() {
return connectionId;
}
public void setConnectionId(String connectionId) {
this.connectionId = connectionId;
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
public String getApplicationVersion() {
return applicationVersion;
}
public void setApplicationVersion(String applicationVersion) {
this.applicationVersion = applicationVersion;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ConnectionMappingPK)) {
return false;
}
ConnectionMappingPK castOther = (ConnectionMappingPK) other;
return this.connectionId.equals(castOther.getConnectionId())
&& this.applicationId.equals(castOther.getApplicationId())
&& this.applicationVersion.equals(castOther
.getApplicationVersion());
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.connectionId.hashCode();
hash = hash * prime + this.applicationVersion.hashCode();
hash = hash * prime + this.applicationId.hashCode();
return hash;
}
}
----------------------------------------- AdminValidator -------- -------------------------------------------------- -----------------------------------------------
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.Demo.persistence.model.DeploymentConfigurationMappingPK ;
public class AdminValidator {
private static final Logger LOG = LoggerFactory
.getLogger(AdminValidator.class);
private ValidatorFactory factory = null;
private Validator validator = null;
public AdminValidator() {
if (factory == null) {
factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
}
public List<String> constraintViolationsForDeploymentConfigMapping(DeploymentConfigurationMappingPK connMapPK) {
if (LOG.isDebugEnabled()) {
LOG.debug("In handler #constraintViolationsDetectedForConnMapping of AdminValidator");
}
List<String> errorMessages = new ArrayList<String>();
**Set<ConstraintViolation<DeploymentConfigurationMappingPK>> constraintViolations = validator**
.validate(connMapPK);
if (constraintViolations.size() > 0)
{
Iterator<ConstraintViolation<DeploymentConfigurationMappingPK>> iterator = constraintViolations
.iterator();
while (iterator.hasNext())
{
ConstraintViolation<DeploymentConfigurationMappingPK> cv = iterator
.next();
errorMessages.add(cv.getMessage());
}
}
return errorMessages;
}
}
---------------- Persistence .xml ------------------------------- -------
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="com.Demo.persistence"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.Demo.persistence.model.DeploymentConfigurationMappingPK</class>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
我们在演示项目中使用了eclipselink和springframework,而我们没有使用Spring MVC。
最佳答案
似乎与错误HV-1013有关,已在 hibernate 5.3.0.CR1中修复。