gpt4 book ai didi

java - Hibernate @Formula 不包含 Schema

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:09 25 4
gpt4 key购买 nike

我有一个具有属性 @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 数据库时,我没有遇到任何问题,但是,当我切换到 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>

注:灵感来自 How to change Hibernate GenerationType identifier depending on the underlying database

注(2):在blog posthere ,作者在运行时生成 PersistenceUnitInfo。

关于java - Hibernate @Formula 不包含 Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51088034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com