jpa CteInsertStrategy只能与支持CTE的方言一起使用,CTE也可以接受UPDATE或DELETE语句

guz6ccqo  于 2022-12-19  发布在  其他
关注(0)|答案(2)|浏览(188)

带有PostgreSQL JDBC驱动程序42.3.5的Hibernate 6.0.1会导致以下异常:

java.lang.UnsupportedOperationException:
CteInsertStrategy can only be used with Dialects that support CTE that can take UPDATE or DELETE statements as well
at org.hibernate.query.sqm.mutation.internal.cte.CteInsertStrategy.<init>(CteInsertStrategy.java:123)
at org.hibernate.query.sqm.mutation.internal.cte.CteInsertStrategy.<init>(CteInsertStrategy.java:107)
at org.hibernate.dialect.PostgreSQLDialect.getFallbackSqmInsertStrategy(PostgreSQLDialect.java:704)
...

出了什么问题?我该如何解决这个问题?
MyEntity.java

import jakarta.persistence.*;

@Entity
@Table(name = "my_entity")
public class MyEntity {

    private Long id;

    @Id
    @SequenceGenerator(name = "id_sequence", sequenceName = "my_id_sequence")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "id_sequence")
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }

}

MyTest.java

import static org.junit.Assert.assertNotNull;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.junit.*;

public class MyTest {

    private static Configuration configuration;
    private static SessionFactory sessionFactory;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        sessionFactory.close();
    }

    private Session session;

    @Before
    public void setUp() throws Exception {
        session = sessionFactory.openSession();
    }

    @After
    public void tearDown() throws Exception {
        session.close();
    }

    @Test
    public void test() {
        Transaction transaction = session.beginTransaction();
        MyEntity entity = new MyEntity();
        session.persist(entity);
        assertNotNull(entity.getId());
        transaction.commit();
    }

}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>  
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mydb</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    <property name="hibernate.c3p0.min_size">1</property>
    <property name="hibernate.c3p0.max_size">30</property>
    <property name="hibernate.c3p0.timeout">120</property>
    <property name="hibernate.c3p0.max_statements">100</property>
    <mapping class="haba713.MyEntity" />
  </session-factory>
</hibernate-configuration>

build.gradle

plugins {
    id 'java-library'
}

repositories {
    mavenCentral()
}

ext {
    hibernateVersion = '6.0.1.Final'
}

dependencies {
    implementation 'org.postgresql:postgresql:42.3.5'
    implementation 'org.hibernate.orm:hibernate-c3p0:' + hibernateVersion
    implementation 'org.hibernate.orm:hibernate-core:' + hibernateVersion
    testImplementation 'junit:junit:4.13.2'
}

请参阅完整的源代码here

a64a0gku

a64a0gku1#

use_jdbc_metadata_defaults配置属性必须为true,Hibernate才能检测到PostgreSQL方言的正确版本。
拆卸此管路

<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

hibernate.cfg.xml解决了该问题。
(感谢Hibernate Zulip channel上的Christian整理出这些信息。)

slmsl1lt

slmsl1lt2#

同样的错误,我得到了当使用错误的休眠方言,在我的情况:org.hibernate.dialect.PostgreSQL9Dialect而不是PostgreSQL v12上的org.hibernate.dialect.PostgreSQLDialect

相关问题