Tom*_*z W 3 java mysql spring hibernate jpa
在我的应用程序中有一个实体:
@Entity
@Table(schema = "hr", name = "personal_data")
public class PersonalData {
}
Run Code Online (Sandbox Code Playgroud)
和Spring的application.properties中定义的连接字符串:
spring.datasource.url=jdbc:mysql://localhost/mobile?UseUnicode=true&characterEncoding=utf8
Run Code Online (Sandbox Code Playgroud)
如果我调用以下代码:
TypedQuery<E> typedQuery = em.createQuery("from PersonalData pd where pd.employeeId = ?1", PersonalData.class);
typedQuery.setParameter(1, 123);
return typedQuery.getSingleResult();
Run Code Online (Sandbox Code Playgroud)
它会导致这个SQL:
select * from personal_data personalda0_ where personalda0_.employee_id=?
Run Code Online (Sandbox Code Playgroud)
哪个会因例外而失败
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mobile.personal_data' doesn't exist
Run Code Online (Sandbox Code Playgroud)
因为该表personal_data是在hr数据库中定义的,并且没有这样的表mobile.
这在Hibernate 4.3.13中运行正常(即SQL中的表名以数据库名称为前缀),并在应用程序迁移到使用Hibernate 5.2.14的Spring Boot 2.0时停止.有没有办法在Hibernate 5.x中实现旧的行为?
我可以说Hibernate 5和MySQL之间存在误解,这里的长篇故事Hibernate 5.0.6忽略了MySQL中的模式
建议的一个解决方案是在目录的位置使用模式的名称,而不是:
@Table(schema = "hr", name = "personal_data")
^^^^^^
Run Code Online (Sandbox Code Playgroud)
您可以使用 :
@Table(catalog = "hr", name = "personal_data")
^^^^^^^
Run Code Online (Sandbox Code Playgroud)
另外看看这个:
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |