Hibernate @Formula不包含架构 [英] Hibernate @Formula doesn't include Schema
问题描述
我有一个具有属性 @Formula 的实体,如下所示:
I have an entity with a property @Formula like this:
@Entity
@Table(name = "areasAuxiliar")
public final class AreaAuxiliar implements Serializable {
@Id
@Column(name = "idArea")
private Integer idArea;
@Formula("RUTAAREA(idArea)")
private String ruta;
当我将休眠配置为指向Oracle DB时,我没有问题, 但是,当我切换到SQLServer时,休眠状态不包含 shema ,并且查询失败,
when I configure my hibernate to point to an Oracle DB I have no problem, BUT, when I switch to an SQLServer, hibernate is not including the shema and the query fails,
为休眠生成的查询如下:
the query generated for hibernate looks like this:
select
areaauxili4_.idArea as idArea1_6_4_,
rutaArea(areaauxili4_.idArea) as formula2_4_
from
SIGAP.areasAuxiliar areaauxili4_
正在读取参数 hibernate.default_schema = SIGAP 并将其包含在表中,但未包含在函数中,
the param hibernate.default_schema=SIGAP is being read and included in the table but not in the function,
在该功能中是否存在用于强制Shema的选项/注释?
is there an option/annotation to force the shema in in that function?
我尝试了休眠5.1和5.2,结果相同:(
I have tried hibernate 5.1 and 5.2 with the same result :(
推荐答案
更简化的解决方案:
从此更改您的@Formula:
change your @Formula from this:
@Formula("RUTAAREA(idArea)")
对此:
@Formula("{MYAPP_SCHEMA}.RUTAAREA(idArea)")
创建一个类:
public class HibernateEntityInterceptor extends EmptyInterceptor {
}
在我的情况下,将其注册为您的sessionFactory中的实体拦截器:
register it as an Entity Interceptor in your sessionFactory, in my case:
sessionFactory.setEntityInterceptor(new HibernateEntityInterceptor());
然后在该类中重写此方法:
then in that class you override this method:
public String onPrepareStatement(String sql) {
该方法在执行之前先接收sql命令,因此,您所需要做的就是简单地替换所有内容:
that method recive the sql command before it is executed, so, all you need to do is a simple replace all:
sql = sql.replaceAll("\\{MYAPP_SCHEMA}", default_schema);
return sql;
谢谢.
这篇关于Hibernate @Formula不包含架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!