gpt4 book ai didi

java - Bean 验证不起作用。我正在使用 JPA 2.0 和 Hibernate 3.3

转载 作者:行者123 更新时间:2023-12-02 07:28:20 25 4
gpt4 key购买 nike

验证适用于前端(jsf)。然而,它尚未在 prepersist 上得到验证。我将制造商设置为 null,并将其存储在数据库中,而不是抛出约束违规异常。谢谢。

这是我的实体的相关部分:

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;
}
}

最佳答案

Hibernate 3.3 不是 JPA2.0 实现。 JPA2.0 需要 Hibernate >=3.5.1。Hibernate 3.3 中没有集成 bean-validation API

要将 bean-validation 集成到 hibernate 3.3 中,您必须实现 BeanValidationEventListener。以 Hibermate 实现为例:

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/BeanValidationEventListener.java

关于java - Bean 验证不起作用。我正在使用 JPA 2.0 和 Hibernate 3.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13255424/

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