验证适用于前端 (jsf)。但是,它没有在 prepersist 上得到验证。我将制造商设置为空,它被存储在数据库中,而不是抛出约束冲突异常。谢谢。
这是我实体的相关部分:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
* The persistent class for the CAR database table.
*
*/
@Entity
@Table(name="CAR")
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CAR_ID")
private int carId;
@Column(name="MANUFACTURER")
@NotNull(message = "A manufacturer is required.")
private String manufacturer;
这是我的 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="src_persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/BEAN_VALIDATION_DS</jta-data-source>
<class>
xxx.beanvalidation.persistence.Car
</class>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="javax.persistence.validation.mode" value="callback"/>
<property name="openjpa.Optimistic" value="false"/>
<property name="openjpa.LockManager" value="none"/>
<property name="openjpa.jdbc.Schema" value="dbo"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.current_session_context_class" value="thread" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
</properties>
</persistence-unit>
</persistence>
这是我的 DAO:
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.westfieldgrp.beanvalidation.persistence.Car;
import com.westfieldgrp.beanvalidation.persistence.Driver;
import com.westfieldgrp.beanvalidation.persistence.Person;
import com.westfieldgrp.common.data.dao.DAOInterface;
public class CarDAO {
private EntityManager entityManager;
public void save(Person convertedPerson, Driver convertedDriver, Car convertedCar) {
EntityManager em = getEntityManager();
//convertedPerson.setName(convertedPerson.getName()); // To test JPA validation
Person savedPerson = em.merge(convertedPerson);
convertedDriver.setPersonId(savedPerson.getPersonId());
Driver savedDriver = em.merge(convertedDriver);
convertedCar.setDriverId(savedDriver.getPersonId());
if("test".equals(convertedCar.getManufacturer())){
convertedCar.setManufacturer(null);// To test JPA validation
}
em.persist(convertedCar);
em.flush();
}
public EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("src_persistence");
entityManager = emf.createEntityManager();
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}