JPA/Hibernate 适用于 Postgresql,但不适用于 Mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9946521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:47:26  来源:igfitidea点击:

JPA/Hibernate works with Postgresql, but not with Mysql

javamysqlhibernatepostgresqljpa

提问by Vojtěch

I am trying to replace PostgreSQL in my application with MySQL. I thought that it should be sufficient to replace the <properties>in persistence.xml file:

我正在尝试用 MySQL 替换我的应用程序中的 PostgreSQL。我认为替换<properties>persistence.xml文件应该就足够了:

PostgreSQL:

PostgreSQL:

<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/postgres"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.show_sql" value="true"/>

MySQL:

MySQL:

<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hiberante.connection.url" value="jdbc:mysql://localhost:3306/GoOut2"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>

But with this replacement, I amg getting

但是有了这个替代品,我得到了

java.lang.UnsupportedOperationException: The application must supply JDBC connections

I am not sure what I am doing wrong, I just hoped that the replacement would be straightforward. In PostgreSQL, everything works correctly.

我不确定我做错了什么,我只是希望替换很简单。在 PostgreSQL 中,一切正常。

Persistence.xml: https://gist.github.com/2252443

Persistence.xml:https: //gist.github.com/2252443

applicationContext.xml: https://gist.github.com/2252463

applicationContext.xml:https: //gist.github.com/2252463

exception: https://gist.github.com/2252487

例外:https: //gist.github.com/2252487

Thanks!

谢谢!

EDIT: I remove username and password from the given code intentionally.

编辑:我有意从给定的代码中删除了用户名和密码。

回答by Pau Kiat Wee

You missing configuration hibernate dialect for PostgreSQL:

您缺少 PostgreSQL 的配置休眠方言:

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>

EDIT:

编辑:

You have miss spelling in configuration:

您在配置中有拼写错误:

<property name="hiberante.connection.url" value="jdbc:mysql://localhost:3306/GoOut2"/>

should be

应该

<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/GoOut2"/>

Is hibernate, but not hiberante.

hibernate,但不是hiberante

回答by Vishal Ranapariya

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>