hibernate 相关问题

Hibernate是Java语言的对象关系映射(ORM)库,使开发人员能够以远远超出对象/关系映射的方式在其应用程序中使用POJO样式的域模型。

使用 JPA 动态更新

我最近惊讶地发现默认的休眠行为是在仅进行一次更改并调用合并时更新对象中的所有字段。 动态更新是领域 t...

回答 3 投票 0

设置上下文 |获取 TransactionSynconizationMananger.isCurrentTransactionReadOnly() 的值

我正在尝试将我的事务动态路由到 READ_WRITE 或 READ_ONLY 数据源。 但在我的类中 TransactionRoutingDataSource 扩展了 AbstractRoutingDataSource 当我试图获得 va 时...

回答 1 投票 0

在Maven中配置hibernate-jpamodelgen

我想将 hibernate-jpamodelgen 配置到 Maven pom.xml 中。我试过这个: 我想将 hibernate-jpamodelgen 配置到 Maven 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>plugin</groupId> <artifactId>org.plugin</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>Plugin</name> <url>http://maven.apache.org</url> <parent> ........ <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.3.Final</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>datalis_plugin</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>10</source> <target>10</target> <encoding>${project.build.sourceEncoding}</encoding> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> </path> </annotationProcessorPaths> <compilerArguments> <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> </compilerArguments> </configuration> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project> 完整 POM:https://pastebin.com/VjucMAYL 但是我收到错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project org.plugin: Compilation failure [ERROR] Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found 你知道我该如何解决这个问题吗? 我用过这个:https://docs.jboss.org/hibernate/jpamodelgen/1.0/reference/en-US/html_single/ 我通常只是将 hibernate-jpamodelgen 添加到编译器插件的注释处理器路径中。这会阻止处理器打包到部署中。 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> <path> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.3.Final</version> </path> </annotationProcessorPaths> </configuration> </plugin> 从 中删除 <scope>provided</scope> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.3.Final</version> </dependency> 添加插件 <plugins> ... <plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>generate-sources</phase> <configuration> <processors> <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> </processors> </configuration> </execution> </executions> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.3.Final</version> </dependency> </dependencies> </plugin> ... </plugins> 然后重建 mvn clean package -DskipTests 这对我有用 pom.xml: <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <target>11</target> <annotationProcessorPaths> <path> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.2.11.RELEASE</version> </path> <path> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.22.Final</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> </dependency> </dependencies> <properties> <spring.profiles.active>dev</spring.profiles.active> </properties> </profile> </profiles> import com.mycompany.myapp.domain.*; // for static metamodels import com.mycompany.myapp.domain.User public class UserQueryService { private Specification<User> createSpecification(UserCriteria criteria) { Specification<User> specification = Specification.where(null); if (criteria != null) { if (criteria.getId() != null) { specification = specification.and(buildSpecification(criteria.getId(), User_.id)); } } return specification; } } 如果是 Java 12,请在 pom.xml 的构建中使用以下代码片段。 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>12</release> </configuration> </plugin> 在此之后,为 jpamodelgen 添加以下内容。 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.3.Final</version> <optional>true</optional> </dependency> 将 hibernate-jpamodelgen 添加到我的依赖项并没有为我完成这项工作,而是创建了依赖项冲突,这导致 maven-enforcer-plugin 的 dependencyConvergence 抱怨,除非我将其与范围“受保护”结合起来。此外,在我看来,声明运行时依赖项似乎不太合理,除非我有一个。 事实证明这对我来说没有必要:我使用了 Cesar 的方法,但与 Cesar 的情况不同,我不需要添加 hibernate-jpamodelgen 或我的依赖项的其他任何内容。 我已经在maven-compiler-plugin中添加了一个处理器,即mapstruct-processor。 就像塞萨尔建议的那样,我将 hibernate-jpamodelgen 添加到该配置中。就我而言,结果是: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.4.2.Final</version> </path> <!-- The following path declaration is the only thing I added to make hibernate-jpamodelgen run --> <path> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.3.20.Final</version> </path> </annotationProcessorPaths> </configuration> </plugin> 使用“maven-compiler-plugin”的正确解决方案。 如果您使用高于 3.1 的版本,请使用标签 compilerArgs。 如果您使用版本低于3.1,请使用标签compilerArguments 不要忘记删除不适合您的版本的相应标签。 为什么还使用“build-helper-maven-plugin”,请检查 Martin Baumgartner 在此线程中的答案。 通过将生成的源放入目标中,您将在 IDE 中遇到问题,因为找不到相关代码。因此,您可以添加 build-helper-maven-plugin 来动态添加目标目录中的文件夹 <build> <directory>${project.basedir}/target</directory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.plugin.compiler.version}</version> <configuration> <annotationProcessorPaths> <path> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-jpamodelgen</artifactId> </path> </annotationProcessorPaths> <compilerArgs> <!-- Use this for plugin version HIGHER than 3.1, delete the other one --> <arg>-processor</arg> <arg>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</arg> </compilerArgs> <compilerArguments> <!-- Use this for plugin version LOWER than 3.1, delete the other one --> <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>${build-helper-maven-plugin.version}</version> <executions> <execution> <id>add-source</id> <phase>process-classes</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>${project.basedir}/target/generated-sources/annotations</source> </sources> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

回答 6 投票 0

Spring boot:无法打破bean之间的依赖循环

我正在使用 Spring Boot 创建一个航空公司网站。我有一个 Seat 类和一个 Flight 类,它们代表数据库中的表。这个想法是,每当添加航班时,座位......

回答 1 投票 0

Hibernate envers:StrictJpaComplianceViolation:遇到 FQN 实体名称

如果我设置 hibernate.jpa.compliance.query=true 那么我得到: org.hibernate.query.sqm.StrictJpaComplianceViolation:遇到 FQN 实体名称 [gbt.osmoze.model.osmoze.audit.CustomRevEntity],但严格

回答 1 投票 0

如何使用Spring data JPA保存双向关联而不进行级联?

假设我在父子之间有双向一对多关联,映射如下: 父级.java: @实体 公开课家长{ @ID 私有整数 ID; @OneToMany(mappedBy...

回答 2 投票 0

如何在 IntelliJ 调试器中显示 Hibernate 3 标准的 sql 查询

我正在开发一个遗留项目,该项目没有像 Maven 或 gradle 那样的任何特定项目结构。我们使用 ant 来构建项目并添加依赖项,就像我们在简单的 java 项目中添加一样......

回答 1 投票 0

通过多个选择进行急切获取,但没有 FetchType.EAGER 注释

我想在某些用例中急切地获取一对多列表(但并非总是如此,所以 @OneToMany(fetch=FetchType.EAGER) 不会很好),并且我也希望 JPA 使用单独的SQL 选择获取那些 Lis...

回答 2 投票 0

JPA Criteria Api 未生成正确的 sql

我的条件查询中有一段,我希望生成一个sql段作为子字符串(manifest_url, instr(manifest_url, '?') - 14, 10) 最终路径manifestUrlPath = JpaUtils.ge...

回答 1 投票 0

Wildfly 32:模式验证错误:找到类型#VARCHAR,但需要类型#ENUM

WIldfly 32 出现此问题。 我在一个可启动的 jar 中构建了我的 Wildfly 环境。在我使用 galleon 将 Wildfly 28 更新为 32 后,当我构建并尝试运行它时,Wildfly 开始出现错误。一个

回答 1 投票 0

将 Wildfly 28 升级到 32:架构验证错误:找到类型#VARCHAR 但需要类型#ENUM

我在可启动的 jar 中构建了我的 Wildfly 环境。在我使用 galleon 将 Wildfly 28 更新为 32 后,当我构建并尝试运行它时,Wildfly 开始出现错误。发生的一个错误是由

回答 1 投票 0

有没有办法将 Hibernate Envers 与自定义查询一起使用

我想问是否有一种方法可以使用自定义查询来查找修订的实体。 原因是我想完全控制我的实体何时加载,有时我也有...

回答 1 投票 0

Spring Data:使用@CreatedBy为ManyToMany关系添加额外的审核属性到JoinTable

在 Spring Data 中为多对多关系向连接表添加附加属性的最佳实践是什么? 更准确地说,我想添加一些用于使用 @Created 进行日志记录的信息...

回答 1 投票 0

表上的插入或更新违反了外键约束,表中不存在键

我正在尝试将一个条目插入到我的数据库中,该条目与另一个实体具有一对一的关系。我正在使用 spring data jpa 和 postgres。 我的代码如下所示: @MappedSuperclass @盖特 @塞特 @

回答 1 投票 0

使用 NamedEntityGraph 进行急切获取返回太多行

我有以下实体: @实体 @NamedEntityGraph(name = "Text.WithRows", attributeNodes = { @NamedAttributeNode("rows") }) 公共课文本{ @ID @Column(name = "uuid", nullable = fal...

回答 2 投票 0

SpringBoot - 我的项目正在使用内存中的数据库,我不知道如何停止这样做并更改为实际的持久数据库

我已经使用 h2 数据库(在内存中)创建了一个 Spring 项目,以便在我开始该项目时使用它。现在,我想在项目完成后使用持久数据。问题是...

回答 4 投票 0

Hibernate/JPA:如何为 CRUD 操作正确建模单向 @ManyToMany 关系?

我已经实现了以下实体: 老师: @盖特 @塞特 @ToString @实体 @Builder @AllArgsConstructor @NoArgs构造函数 公开课老师{ @ID @GenerateValue(策略 =

回答 1 投票 0

在 Hibernate 中插入/更新一个实体与另一个引用的实体

当在hibernate中插入/更新实体并且只想更新对更新实体的更改时,是否可以只设置保存引用实体的外键ID?例如。 用户角色 userRol...

回答 2 投票 0

使用 hibernate-validator 基于另一个字段验证 Java 中的一个字段的最佳方法是什么?

我有一个由许多字段组成的类,这些字段具有 hibernate-validator 注释,因此如果输入无效数据,它可以抛出相应的消息。 一些字段是

回答 2 投票 0

Hibernate 验证:使用户能够仅输入数字(数字)而不是输入

我有一个 jsf 表单,并且有一个通常只接受 number(integer) 的输入。 我想自定义当用户在此字段中输入字符串或字符时的错误消息。我想要验证...

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.