Hibernate @Formula doesn't include Schema

Alfredo M :

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;

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_ 

the param hibernate.default_schema=SIGAP is being read and included in the table but not in the function,

is there an option/annotation to force the shema in in that function?

I have tried hibernate 5.1 and 5.2 with the same result :(

Alfredo M :

a more simplified solution:

change your @Formula from this:

@Formula("RUTAAREA(idArea)")

to this:

@Formula("{MYAPP_SCHEMA}.RUTAAREA(idArea)")

create a class:

public class HibernateEntityInterceptor extends EmptyInterceptor {

}

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) {

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;

thanx for the help.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=442999&siteId=1