【问题标题】:CteInsertStrategy can only be used with Dialects that support CTE that can take UPDATE or DELETE statements as wellCteInsertStrategy can only be used with Dialects that support CTE that can take UPDATE or DELETE statements as well
【发布时间】:2022-12-19 04:36:20
【问题描述】:

Hibernate 6.0.1 with PostgreSQL JDBC driver 42.3.5 causes the following exception:

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)
...

What's wrong and how can I fix the issue?

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

See the full source code here.

【问题讨论】:

    标签: java postgresql hibernate jpa common-table-expression


    【解决方案1】:

    The use_jdbc_metadata_defaults configuration property must be true for Hibernate to detect the correct version of the PostgreSQL dialect.

    Removing this line

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

    from hibernate.cfg.xml resolves the issue.

    (Thanks to Christian at Hibernate Zulip channel for sorting this out.)

    【讨论】:

    • In my case the cause was simpler and less obvious: wrong password for connecting user
    【解决方案2】:

    The same error i got when used wrong hibernate dialect, in my case: org.hibernate.dialect.PostgreSQL9Dialect instead of org.hibernate.dialect.PostgreSQLDialect on PostgreSQL v12.

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2022-12-01
      • 2023-01-05
      • 2017-10-19
      • 2022-12-02
      • 2022-12-26
      • 2022-12-02
      • 2015-01-23
      • 2022-12-02
      相关资源
      最近更新 更多