我是Spring Jpa和Hibernate的新手。我正嘗試從Oracle數據庫中使用自定義函數來獲取數據。我可以定義一個實體及其相關的服務,實現和存儲庫。另外,我將通過使用registerFunction
創建一個新的自定義Oracle方言,如下所示。如何在jpa查詢中使用自定義函數?
所以我有兩個問題:
1)在我的Oracle數據庫,該功能位於不同的模式下。我需要指定它的模式嗎?如果是這樣如何?或者休眠會自動找到它?
我會要求我在文章的最後第二個問題提供我的全堆棧跟蹤後...
這裏是我完整的堆棧跟蹤:
MyOracle10gDialect
package blog;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.function.StandardSQLFunction;
public class MyOracle10gDialect extends Oracle10gDialect {
public MyOracle10gDialect() {
super();
registerFunction("my_function", new StandardSQLFunction("my_function"));
}
}
application.properties
...
spring.jpa.database-platform=blog.MyOracle10gDialect
...
實體:
package blog.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "item", schema = "WOS_SOURCE")
public class WosItem {
@Id
@Column(nullable = false)
private String UT;
@Column(nullable = false)
private String TI;
public String getUT() {
return UT;
}
public void setUT(String UT) {
this.UT = UT;
}
public String getTI() {
return TI;
}
public void setTI(String TI) {
this.TI = TI;
}
public WosItem(String UT, String TI) {
this.UT = UT;
this.TI = TI;
}
public WosItem() { }
@Override
public String toString() {
return "WosItem{" +
"UT='" + UT + '\'' +
", TI='" + TI + '\'' +
'}';
}
}
服務:
package blog.services;
import blog.models.WosItem;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface WosItemService {
List<WosItem> findAll();
WosItem findById(String id);
String find_ut(Long ut_seq);
}
實現:
package blog.services;
import blog.models.WosItem;
import blog.repositories.WosItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WosItemServiceJpaImpl implements WosItemService {
@Autowired
private WosItemRepository wosItemRepository;
@Override
public List<WosItem> findAll() {
return this.wosItemRepository.findAll();
}
@Override
public WosItem findById(String id) {
return this.wosItemRepository.findOne(id);
}
@Override
public String find_ut(Long ut_seq) {
return this.wosItemRepository.find_ut();
}
}
庫:
package blog.repositories;
import blog.models.WosItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface WosItemRepository extends JpaRepository<WosItem, String> {
@Query("SELECT function('my_function', input) FROM WosItem wos");
String find_ut();
}
所以在我的Oracle數據庫,我可以使用此功能,如下圖所示:
select other_schema.my_function(aa.input) from my_schema.TABLE aa;
對於離。說aa.input是332708100009
則返回000332708100009
至於我的第二個問題:
2)我怎樣才能開展JPA這個過程?我知道我的存儲庫根本不正確。我收到了一個錯誤,如「註釋不允許在這裏」。我找不到解決方法。
在此先感謝。
請編輯拋出的異常:
Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\-[METHOD_CALL] MethodNode: 'function (my_function)'
+-[METHOD_NAME] IdentNode: 'my_function' {originalText=my_function}
\-[EXPR_LIST] SqlNode: 'exprList'
\-[NAMED_PARAM] ParameterNode: '?' {name=ut_seq, expectedType=null}
註釋不要求以';'結尾。只要刪除它。 –
謝謝! @ M.Deinum現在我有上面更新的錯誤。我如何調用我的存儲庫中的輸入值? –
你在'WosItem'上似乎沒有'input'列。你可以發佈你的確切代碼而不需要簡化它嗎? – Strelok