Good day!
I need to use some unsupported (Oracle) functions, for example, LOCATE. In the Oracle9Dialect this function declared in the following way:
Code:
registerFunction( "locate", new SQLFunctionTemplate( Hibernate.INTEGER, "instr(?2,?1)" ) );
I believe that this should mean:
Code:
select instr('programmer', 'program') === select locate('program', 'programmer');
But I can't to use this feature!
Assume that I have a table:
Code:
create table test_1 (id int, description varchar(255))
I need to make a query like this:
Code:
select id, locate(description, 'program') from table_test;
To do so, I use the following class:
Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Formula;
@Entity
@Table(name = "TEST_1")
public class Test_1_DTO {
public Test_1_DTO() {
super();
}
private Integer id;
private String desc;
@Column(name = "DESCRIPTION", length = 255)
@Formula(value = "locate(description, 'program')")
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Id
@Column(name = "ID")
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Test_1_DTO(Integer id, String desc) {
super();
this.id = id;
this.desc = desc;
}
}
For reception of sample I execute:
Code:
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
...
AnnotationConfiguration aconf = new AnnotationConfiguration();
Configuration cfg = aconf.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Test_1_DTO obj = session.load(Test_1_DTO.class, 1);
session.getTransaction().commit();
I expect to receive:
Code:
select test_1_dto0_.ID as ID0_0_, instr('program', test_1_dto0_.description) as formula0_0_ from TEST_1 test_1_dto0_ where test_1_dto0_.ID=?
Instead of this the result is:
Code:
select test_1_dto0_.ID as ID0_0_, test_1_dto0_.ddd as ddd0_0_, locate(test_1_dto0_.description, 'program') as formula0_0_ from TEST_1 test_1_dto0_ where test_1_dto0_.ID=?
I.e. LOCATE not replaced by INSTR!
What am I doing wrong?