当前位置: 首页 > 知识库问答 >
问题:

使用hibernate和spring数据jpa和mysql编写主键

萧业
2023-03-14

我试图测试一个组合的主键,不幸的是有些东西运行不正确。

包含组合主键的课程类

@Entity(name = "Course")
public class Course {
    @EmbeddedId
    private PkCourse pkCourse;

    public Course() {}

    public Course( PkCourse pkCourse) {
        this.pkCourse = pkCourse;
    }
    public PkCourse getPkCourse() {
        return pkCourse;
    }
    public void setPkCourse( PkCourse pkCourse ) {
        this.pkCourse = pkCourse;
    }
}

表示组合主键的类

@Embeddable
public class PkCourse implements Serializable {

    @Column(name = "courseName")
    private String courseName;

    @Column(name = "courseGrade")
    private Integer grade;

    PkCourse(){
    }
    public PkCourse( String courseName, Integer grade ) {
        this.courseName = courseName;
        this.grade = grade;
    }

    public String getCourseName() {
        return courseName;
    }
    public void setCourseName( String courseName ) {
        this.courseName = courseName;
    }

    public Integer getGrade() {
        return grade;
    }
    public void setGrade( Integer grade ) {
        this.grade = grade;
    }
    @Override
    public boolean equals( Object o ) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PkCourse pkCourse = (PkCourse) o;
        return courseName.equals(pkCourse.courseName) &&
                Objects.equals(grade, pkCourse.grade);
    }
    @Override
    public int hashCode() {
        return Objects.hash(courseName, grade);
    }
}

我的测试程序

private void composedPrimaryKey() {
        PkCourse composedPK = new PkCourse("database", 3);
        Course course = new Course(composedPK);
        courseRepository.save(course);
    }

我的 jpa repository

public interface CourseRepository extends JpaRepository<Course, PkCourse> {
}

我得到的例外

原因:org.springframework.dao.InvalidDataAccessResourceUsageException: 無法提取 ResultSet;SQL [n/a];嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet

原因:Java . SQL . sqlsyntaxerrorexception:表“company.course”不存在

完整的痕迹

> java.lang.IllegalStateException: Failed to execute CommandLineRunner
>   at
> org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:783)
> [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]     at
> org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:764)
> [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]     at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:319)
> [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]     at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
> [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]     at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
> [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]     at
> jpa.hibearnate.example.hibernateDemo4.HibernateDemoApplication.main(HibernateDemoApplication.java:26)
> [classes/:na]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:1.8.0_131]   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:1.8.0_131]   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:1.8.0_131]   at java.lang.reflect.Method.invoke(Method.java:498)
> ~[na:1.8.0_131]   at
> org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
> [spring-boot-devtools-2.1.7.RELEASE.jar:2.1.7.RELEASE] Caused by:
> org.springframework.dao.InvalidDataAccessResourceUsageException: could
> not extract ResultSet; SQL [n/a]; nested exception is
> org.hibernate.exception.SQLGrammarException: could not extract
> ResultSet     at
> org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:279)
> ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:253)
> ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
> ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
> ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]  at
> org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
> ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]  at
> org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
> ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:144)
> ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$ExposeRepositoryInvocationInterceptor.invoke(CrudMethodMetadataPostProcessor.java:364)
> ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> com.sun.proxy.$Proxy71.save(Unknown Source) ~[na:na]  at
> jpa.hibearnate.example.hibernateDemo4.HibernateDemoApplication.composedPrimaryKey(HibernateDemoApplication.java:37)
> [classes/:na]     at
> jpa.hibearnate.example.hibernateDemo4.HibernateDemoApplication.run(HibernateDemoApplication.java:31)
> [classes/:na]     at
> org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:780)
> [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]     ... 10 common frames
> omitted Caused by: org.hibernate.exception.SQLGrammarException: could
> not extract ResultSet     at
> org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.getResultSet(Loader.java:2167)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1930)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1892)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.doQuery(Loader.java:937)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:310)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.Loader.loadEntity(Loader.java:2281)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:64)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:54)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4273)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:511)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:481)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:222)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:281)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:124)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1257)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:208)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2881)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2855)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.get(SessionImpl.java:1098)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:290)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:901)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.merge(SessionImpl.java:887)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ~[na:1.8.0_131]   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:1.8.0_131]   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:1.8.0_131]   at java.lang.reflect.Method.invoke(Method.java:498)
> ~[na:1.8.0_131]   at
> org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:309)
> ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> com.sun.proxy.$Proxy69.merge(Unknown Source) ~[na:na]     at
> org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:538)
> ~[spring-data-jpa-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ~[na:1.8.0_131]   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:1.8.0_131]   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:1.8.0_131]   at java.lang.reflect.Method.invoke(Method.java:498)
> ~[na:1.8.0_131]   at
> org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
> ~[spring-data-commons-2.1.10.RELEASE.jar:2.1.10.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
> ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]  at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
> ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]     at
> org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
> ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]  ... 24 common frames
> omitted Caused by: java.sql.SQLSyntaxErrorException: Table
> 'company.course' doesn't exist    at
> com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
> ~[mysql-connector-java-8.0.17.jar:8.0.17]     at
> com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
> ~[mysql-connector-java-8.0.17.jar:8.0.17]     at
> com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
> ~[mysql-connector-java-8.0.17.jar:8.0.17]     at
> com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
> ~[mysql-connector-java-8.0.17.jar:8.0.17]     at
> com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
> ~[mysql-connector-java-8.0.17.jar:8.0.17]     at
> com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
> ~[HikariCP-3.2.0.jar:na]  at
> com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
> ~[HikariCP-3.2.0.jar:na]  at
> org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]

编辑1

我通过用一个字符串属性替换整数属性< code>grade解决了创建课程表的问题。

编辑2

我的Hibernate日志:

> Hibernate: 
>     
>     drop table if exists course Hibernate: 
>     
>     create table course (
>        course_name varchar(255) not null,
>         course_grade integer not null,
>         primary key (course_name, course_grade)
>     ) engine=MyISAM 2019-08-15 09:36:34.238  WARN 11508 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     :
> GenerationTarget encountered exception accepting command : Error
> executing DDL "
>     create table course (
>        course_name varchar(255) not null,
>         course_grade integer not null,
>         primary key (course_name, course_grade)
>     ) engine=MyISAM" via JDBC Statement
> 
> org.hibernate.tool.schema.spi.CommandAcceptanceException: Error
> executing DDL "
>     create table course (
>        course_name varchar(255) not null,
>         course_grade integer not null,
>         primary key (course_name, course_grade)
>     ) engine=MyISAM" via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytes

Hibernate: 
    select
        course0_.course_name as course_n1_0_0_,
        course0_.course_grade as course_g2_0_0_ 
    from
        course course0_ 
    where
        course0_.course_name=? 
        and course0_.course_grade=?
2019-08-15 09:36:34.718 ERROR 11508 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : Table 'company.course' doesn't exist

有人能解释一下为什么不同类型的两个属性不能工作吗?(在我的第一个例子中是字符串和整数)。

编辑3

另外,我通过修改注释解决了我的问题

@Column(name = "courseName")
private String courseName;

@Column(name = "courseName", length = 20)
private String courseName;

我尝试了基于异常行的此修改

原因:java.sql.SQLSyntaxErrorException:指定的密钥太长;最大密钥长度为1000字节

有人能解释一下这背后的魔力吗?

共有1个答案

毛弘博
2023-03-14

如果你看看DDL

create table course (
  course_name varchar(255) not null,
  course_grade integer not null,
  primary key (course_name, course_grade)
) engine=MyISAM" via JDBC Statement

MyISAM表中索引的值的大小有限制,此默认大小也是MySQL的相关版本

现在,创建此类表的理想方法不是使用course_namecourse_grade作为主键,而是应该使用uuid或auto gen id,然后在course_namecourse_grade的组合上放置唯一索引。

如果您仍然遇到问题,您还可以尝试此答案中提到的线程中提到的其他修复程序

> < Li > < p > < code > SET @ @ global . innodb _ large _前缀= 1;

升级到更高的 MySQL 版本 5.7

设置全局存储_engine='InnoDb'

参考

#1071-指定的密钥太长;最大密钥长度为1000字节

#1071 -指定的密钥太长;最大密钥长度为767字节

https://dba.stackexchange.com/questions/49913/specified-key-was-too-long-max-key-length-is-1000-bytes-in-mysql-5-6

 类似资料:
  • 问题内容: 是否可以使用Spring Data JPA(由Hibernate作为JPA提供者支持)并同时直接使用Hibernate? 问题是,当我使用JpaTransactionManager时,无法使用检索当前会话。当我切换到HibernateTransaction Manager时,JPA存储库无法提交更改。 这是我的Spring上下文的一部分(在该上下文中,我无法使用直接的Hibernate

  • 我使用Spring Data、JPA和Hibernate作为持久性提供程序进行了以下设置。我的所有实体都继承自基类 例如:

  • 我知道SpringDataJPA使用Hibernate,但是,我有一些问题需要澄清。 > 与使用hibernate会话和HQL相比,使用@Query编写自定义查询有哪些优势? 使用SpringDataJParepository查询方法和使用HibernateHQL之间有什么性能差异吗? 我在这里看到了其他类似的问题,他们没有回答这些问题。

  • 我有一个带有spring Boot(v2.3.3)和spring数据的spring web应用程序。我的评估表包含以下列: Id 我的用例是表中名称和地址的任何更改评估应该创建一个新行,版本以相同的id递增。 因此,基本上只有当地址或姓名更新时,才能在评估表中插入新记录。下面是一个示例: 现有记录: 例如,名称已更新为Ryan,应该有两行,如下所示: 因此,基本上,任何名称和地址的变化都应在评估表

  • 由于我是审计新手,请让我了解以上问题。 谢谢你。

  • 是否可以使用Spring数据JPA(以Hibernate作为JPA提供程序为后盾),同时直接使用Hibernate? 问题是,当我使用JpaTransactionManager时,我无法使用检索当前会话。当我切换到HibernateTransaction管理器时,JPA存储库无法提交更改。 下面是Spring上下文的部分(对于该上下文,我无法使用直接的Hibernate调用): hibernate