我有一个拥有财产的实体@Formula像这样:
@Entity
@Table(name = "areasAuxiliar")
public final class AreaAuxiliar implements Serializable {
@Id
@Column(name = "idArea")
private Integer idArea;
@Formula("RUTAAREA(idArea)")
private String ruta;
当我将 hibernate 配置为指向 Oracle DB 时,我没有问题,
但是,当我切换到 SQLServer 时,hibernate 不包括shema并且查询失败,
为 hibernate 生成的查询如下所示:
select
areaauxili4_.idArea as idArea1_6_4_,
rutaArea(areaauxili4_.idArea) as formula2_4_
from
SIGAP.areasAuxiliar areaauxili4_
参数hibernate.default_schema=SIGAP正在被读取并包含在表中,但不包含在函数中,
是否有选项/注释可以在该函数中强制使用 shema?
我尝试过 hibernate 5.1 和 5.2,结果相同:(
您可以使用mysql-orm.xml
文件来覆盖您的公式,然后配置您的构建以在数据库为 mysql 时考虑该文件。
重写这里的公式:
<entity-mappings
xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm orm_2_1.xsd"
version="2.1">
<package>com.acme.persistence</package>
<entity class="AreaAuxiliar" access="FIELD">
<attributes>
<property name="ruta" formula="schemaName.RUTAAREA(idarea)"/>
</attributes>
</entity>
</entity-mappings>
然后将引用添加到特定的persistence.xml
。然后,您可以在构建中或运行时使用此文件覆盖默认的 persistence.xml(参见下面的链接)。
<persistence
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="persistenceUnit">
<provider>
org.hibernate.jpa.HibernatePersistenceProvider
</provider>
<mapping-file>
mappings/identifier/global/mysql-orm.xml
</mapping-file>
<class>
com.acme.persistence.AreaAuxiliar
</class>
</persistence-unit>
注:深受启发如何根据底层数据库更改 Hibernate GenerationType 标识符 https://stackoverflow.com/questions/50894094/how-to-change-hibernate-generationtype-identifier-depending-on-the-underlying-da
注(2):在博客文章 https://vladmihalcea.com/how-to-replace-the-table-identifier-generator-with-either-sequence-or-identity-in-a-portable-way/ and here https://vladmihalcea.com/how-to-bootstrap-hibernate-without-the-persistence-xml-file/,作者在运行时生成 PersistenceUnitInfo。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)