GVKun编程网logo

与Bean验证API结合使用时,Hibernate是否不遵循JPA规范?(bean之间的关系)

7

本篇文章给大家谈谈与Bean验证API结合使用时,Hibernate是否不遵循JPA规范?,以及bean之间的关系的知识点,同时本文还将给你拓展Hibernate/JPA-注释bean方法vs字段、H

本篇文章给大家谈谈与Bean验证API结合使用时,Hibernate是否不遵循JPA规范?,以及bean之间的关系的知识点,同时本文还将给你拓展Hibernate / JPA-注释bean方法vs字段、Hibernate / JPA-注释Bean方法与字段、Hibernate Search 5.6.0.Alpha3 发布,Hibernate搜索框架、Hibernate 不是开源的么?那么为什么 hibernate-jpa-2.1 这个包没有源码呢?等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

与Bean验证API结合使用时,Hibernate是否不遵循JPA规范?(bean之间的关系)

与Bean验证API结合使用时,Hibernate是否不遵循JPA规范?(bean之间的关系)

这个问题是该问题的后续解决方案:JPAConstraintViolation与回滚

我对JPA和验证API(JSR-303)的组合进行了一些测试。

我在JPA规范中找到了以下内容(第101-102页):

默认情况下,默认Bean验证组(默认组)将在持久化和更新前生命周期验证事件时进行验证

如果validate方法返回的ConstraintViolation对象集不为空,则持久性提供程序必须抛出javax.validation.ConstraintViolationException,其中包含对返回的ConstraintViolation对象集的引用,并且必须将事务标记为回滚。

我设置以下测试:

  • HibernateValidator作为JSR-303实现
  • 2 PersistenceProvider Hibernate和EclipseLink
  • 一个实体NameNotNullWithDefaultGeneratedStrategy,其ID由默认策略(@Generated)和@NotNull String name列生成
  • 另一个实体NameNotNullWithTableGeneratedStrategy,其ID由表格策略(@TableGenerated)和@NotNull String name列生成
  • 测试尝试对persist每个实体的一个实例使用null name
  • 预期的结果javax.validation.ConstraintViolationException由persist方法抛出,并且事务标记为rollback only(即,这些假设基于本文引用的JPA规范)。

结果是:

  • 使用eclipse链接作为提供者:
    • persist方法javax.validation.ConstraintViolationException对两个实体都引发一个。
    • rollback only两种情况下交易均标记为
  • 使用hibernate作为提供者:
    • persist引发一个javax.validation.ConstraintViolationExceptionfor实体NameNotNullWithDefaultGeneratedStrategy+标记为的交易rollback only
    • persist不要为 未标记* 为的实体NameNotNullWithTableGeneratedStrategy+事务抛出任何异常 *rollback only
    • commit因为NameNotNullWithTableGeneratedStrategy失败RollbackException

问题是:

  • 确实违反了JPA规范吗?还是我缺少表生成策略的特殊情况?
  • 万一发生违规:是否有与之相关的现有错误报告?

这是我测试的代码:

package com.example.jpa.validator;import org.junit.Assert;import org.junit.Test;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.EntityTransaction;import javax.persistence.Persistence;import javax.persistence.RollbackException;public class ConstraintViolationExceptionTest {    @Test    public void testHibernateDefaultStrategy() {  // Success        testPersistWithNullName("pu-hibernate",new NameNotNullWithDefaultGeneratedStrategy());    }    @Test    public void testHibernateTableStrategy() {        testPersistWithNullName("pu-hibernate",new NameNotNullWithTableGeneratedStrategy());        //this test fail with :        //java.lang.AssertionError: Expecting a javax.validation.ConstraintViolationException, but persist() succeed !    }    @Test    public void testEclipseLinkDefaultStrategy() {  // Success        testPersistWithNullName("pu-eclipselink",new NameNotNullWithDefaultGeneratedStrategy());    }    @Test    public void testEclipseLinkTableStrategy() {  // Success        testPersistWithNullName("pu-eclipselink",new NameNotNullWithTableGeneratedStrategy());    }    private void testPersistWithNullName(String persistenceUnitName, Object entity){        EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);        EntityManager entityManager = emf.createEntityManager();        try {            final EntityTransaction transaction = entityManager.getTransaction();            transaction.begin();            try {                try {                    entityManager.persist(entity);                    Assert.fail("Expecting a javax.validation.ConstraintViolationException, but persist() succeed !");                } catch (javax.validation.ConstraintViolationException cve) {                    //That''s expected                    Assert.assertTrue("According JPA specs transaction must be flagged as rollback only",transaction.getRollbackOnly());                } catch (Exception e) {                    Assert.assertTrue("According JPA specs transaction must be flagged as rollback only",transaction.getRollbackOnly());                    e.printStackTrace();                    Assert.fail("Expecting a javax.validation.ConstraintViolationException, but got " + e.getClass());                }                transaction.commit();                Assert.fail("persisted with null name !!!");            } catch (RollbackException e) {                //That''s expected            }  catch (Exception e) {                e.printStackTrace();                Assert.fail("Unexpected exception :"+e.getMessage());            }        } finally {            entityManager.close();        }    }}

实体

默认策略

package com.example.jpa.validator;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.validation.constraints.NotNull;@Entitypublic class NameNotNullWithDefaultGeneratedStrategy {    @Id @GeneratedValue private Long id;    @NotNull public String name;    public NameNotNullWithDefaultGeneratedStrategy() {}}

表策略:

package com.example.jpa.validator;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.TableGenerator;import javax.validation.constraints.NotNull;@Entitypublic class NameNotNullWithTableGeneratedStrategy {    @GeneratedValue(strategy = GenerationType.TABLE,        generator = "NAME_MUST_NOT_BE_NULL_ID_GENERATOR")    @TableGenerator(name = "NAME_MUST_NOT_BE_NULL_ID_GENERATOR")    @Id @NotNull private Long id;    @NotNull public String name;    public NameNotNullWithTableGeneratedStrategy() {}}

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="pu-hibernate" transaction-type="RESOURCE_LOCAL">            <provider>org.hibernate.ejb.HibernatePersistence</provider>            <class>com.example.jpa.validator.NameNotNullWithTableGeneratedStrategy</class>            <class>com.example.jpa.validator.NameNotNullWithDefaultGeneratedStrategy</class>            <properties>                <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>                <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test_mem_hibernate"/>                <property name="hibernate.hbm2ddl.auto" value="create-drop"/>                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>            </properties>        </persistence-unit>        <persistence-unit name="pu-eclipselink" transaction-type="RESOURCE_LOCAL">            <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>            <class>com.example.jpa.validator.NameNotNullWithTableGeneratedStrategy</class>            <class>com.example.jpa.validator.NameNotNullWithDefaultGeneratedStrategy</class>            <properties>                <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>                <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test_mem"/>                <property name="eclipselink.ddl-generation" value="create-tables"/>                <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform"/>            </properties>        </persistence-unit>    </persistence>

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>    <project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">        <modelVersion>4.0.0</modelVersion>        <groupId>com.example</groupId>        <artifactId>com.example.jpa.validator</artifactId>        <version>1.0-SNAPSHOT</version>        <properties>            <hibernate.version>4.2.0.CR1</hibernate.version>            <hibernate-validator.version>4.3.1.Final</hibernate-validator.version>            <junit.version>4.11</junit.version>            <h2.version>1.3.170</h2.version>        </properties>        <dependencies>            <dependency>                <groupId>org.hibernate</groupId>                <artifactId>hibernate-validator</artifactId>                <version>${hibernate-validator.version}</version>            </dependency>            <dependency>                <groupId>com.h2database</groupId>                <artifactId>h2</artifactId>                <version>${h2.version}</version>                <scope>test</scope>            </dependency>            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <scope>test</scope>                <version>${junit.version}</version>            </dependency>            <dependency>                <groupId>org.hibernate</groupId>                <artifactId>hibernate-core</artifactId>                <version>${hibernate.version}</version>            </dependency>            <dependency>                <groupId>org.hibernate</groupId>                <artifactId>hibernate-entitymanager</artifactId>                <version>${hibernate.version}</version>            </dependency>            <dependency>                <groupId>org.eclipse.persistence</groupId>                <artifactId>org.eclipse.persistence.jpa</artifactId>                <version>2.4.0</version>            </dependency>            <dependency>                <groupId>org.eclipse.persistence</groupId>                <artifactId>javax.persistence</artifactId>                <version>2.0.0</version>            </dependency>        </dependencies>        <repositories>            <repository>                <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>                <id>eclipselink</id>                <layout>default</layout>                <name>Repository for library EclipseLink (JPA 2.0)</name>            </repository>        </repositories>    </project>

答案1

小编典典

我为此提交了一个错误报告:https :
//hibernate.onjira.com/browse/HHH-8028

Hibernate / JPA-注释bean方法vs字段

Hibernate / JPA-注释bean方法vs字段

我有一个关于Hibernate使用的简单问题。我通过注释类的字段以及注释相应bean的get方法,以两种方式之一不断看到人们使用JPA注释。

我的问题如下:使用JPA注释(例如@Id),注释字段和bean方法之间是否有区别?

例:

@Entity
public class User
{

**@ID**
private int id;

public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

- - - - - -要么 - - - - - -

@Entity
public class User
{


private int id;

**@ID**
public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

Hibernate / JPA-注释Bean方法与字段

Hibernate / JPA-注释Bean方法与字段

我有一个关于Hibernate使用的简单问题。我通过注释类的字段以及注释相应bean的get方法,以两种方式之一不断看到人们使用JPA注释。

我的问题如下:使用JPA注释,例如@Id,注释字段和bean方法之间是否有区别。

例:

@Entity
public class User
{

**@ID**
private int id;

public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

- - - - - -要么 - - - - - -

@Entity
public class User
{


private int id;

**@ID**
public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

Hibernate Search 5.6.0.Alpha3 发布,Hibernate搜索框架

Hibernate Search 5.6.0.Alpha3 发布了,Hibernate Search的作用是对数据库中的数据进行检索的。它是hibernate对著名的全文检索系统Lucene的一个集成方案,作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,从而加快了对内容庞大字段进行模糊搜索的速度(sql语句中like匹配)。

更新日志:

  • It indexes your domain model with the help of a few annotations.

  • Takes care of database/index synchronization.

  • Brings back regular managed objects from free text queries.

版本要求:

  • Hibernate ORM 5.0.x

  • Apache Lucene 5.5.x

项目分为几个Maven模块:

  • backends: Remote backends receiving an indexing job and executing it via different protocols.

  • build-config: Code related artefacts like checkstyle rules.

  • distribution: Builds the distribution package.

  • documentation: The project documentation.

  • engine: The engine of the project. Most of the beef is here.

  • integrationtest: Integration tests with various technologies like WildFly, Spring or Karaf. Also includes performance tests.

  • modules: Integration with WildFly using JBoss Modules.

  • orm: Native integration for Hibernate ORM.

  • serialization: Serialization code used by remote backends.

  • testing: Various helper classes to write tests using Hibernate Search. This module is semi private.

下载地址:https://sourceforge.net/projects/hibernate/files/latest/download

Hibernate 不是开源的么?那么为什么 hibernate-jpa-2.1 这个包没有源码呢?

Hibernate 不是开源的么?那么为什么 hibernate-jpa-2.1 这个包没有源码呢?

Hibernate 不是开源的么?那么为什么 hibernate-jpa-2.1 这个包没有源码呢?

关于与Bean验证API结合使用时,Hibernate是否不遵循JPA规范?bean之间的关系的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Hibernate / JPA-注释bean方法vs字段、Hibernate / JPA-注释Bean方法与字段、Hibernate Search 5.6.0.Alpha3 发布,Hibernate搜索框架、Hibernate 不是开源的么?那么为什么 hibernate-jpa-2.1 这个包没有源码呢?等相关知识的信息别忘了在本站进行查找喔。

本文标签: