CriteriaBuilder调用select函数。

huangapple go评论87阅读模式
英文:

CriteriaBuilder call a select function

问题

我有一个查询我想调用一个函数而不向函数传递参数
示例...

    CREATE OR REPLACE function demo_fn()
    RETURNS TABLE (
        id bigint,
        somename varchar,
        something varchar) AS $$
    BEGIN
        RETURN QUERY SELECT DISTINCT c.id, c.somename, c.something FROM pluto c
        ORDER BY id ASC LIMIT 10;
    END;
    $$
    LANGUAGE plpgsql;

这个查询函数像这样被调用:`select * from demo_fn();`,在数据库中可以正常工作
另一方面调用函数的方法是这样的...

    CriteriaBuilder cb = em.getCriteriaBuilder();
    cb.function("demo_fn", PlutoDTO.class, cb.parameter(null)));
    CriteriaQuery cq = cb.createQuery(PlutoDTO.class);
    Root root = cq.from(Pluto.class);

    TypedQuery q = em.createQuery(cq);
    List<PlutoDTO> pluto = q.getResultList();

当我运行函数时
`Caused by: java.lang.IllegalArgumentException: Error occurred validating the Criteria`
有人能指点我正确的方向吗
真的看不清楚怎么解决这个问题...非常感谢
英文:

I have a query that i want to call a function without parsing a parameter to the function.
Example....

CREATE OR REPLACE function demo_fn()
RETURNS TABLE (
	id bigint,
 somename varchar,
 something varchar) AS $$
BEGIN
	RETURN QUERY SELECT DISTINCT c.id, c.somename , c.something FROM pluto c
	ORDER BY id ASC LIMIT 10; 
END;
$$ 
LANGUAGE plpgsql;

This query function is called like this select * from demo_fn(); which work perfectly well from the database.
On the other hand, the method calling the function is like this....

CriteriaBuilder cb = em.getCriteriaBuilder();
cb.function(&quot;demo_fn&quot;, PlutoDTO.class, cb.parameter(null))));
CriteriaQuery cq = cb.createQuery(PlutoDTO.class);
Root root = cq.from(Pluto.class);

TypedQuery q = em.createQuery(cq);
List&lt;PlutoDTO&gt; pluto = q.getResultList();

When i run the function
Caused by: java.lang.IllegalArgumentException: Error occurred validating the Criteria

Can someone please point me to the right direction
Can't really see my way out here.....much appreciated

答案1

得分: 0

看起来通过条件调用函数有点有趣,所以我所做的是,我使用了一个预处理语句而不是...

Session session = entityManager.unwrap(Session.class); // 从Hibernate获取的Session
session.doWork(new Work() {
    @Override
    public void execute(Connection connection) throws SQLException {
        PreparedStatement ps = connection.prepareStatement("select * from demo_fn()");
        ResultSet rs = ps.executeQuery();
        List<DemoDto> demoDtos = new ArrayList<>();
        while (rs.next()) {
            DemoDto demoDto = new DemoDto();
            demoDto.setDemoValueOne(rs.getString("Column_name_here"));
            demoDtos.add(demoDto);
            // 从ResultSet中获取值,在这种情况下是rs
        }
        // 有一个处理这部分的实现
        // rs.close();
        // ps.close();
    }
});

希望将来对某人有所帮助,这确实是一个令人头疼的问题。不过在分页时要小心,它确实非常灵活。

英文:

So it seems calling a function with criteria gets a little interesting, so what i did was, i used a prepared statement rather...

Session session = entityManager.unwrap(Session.class); //Session from Hibernate
		session.doWork(new Work() {
            @Override
			public void execute(Connection connection) throws SQLException {
				PreparedStatement ps= connection.prepareStatement(&quot;select * from demo_fn()&quot;);
                ResultSet rs= ps.getResultSet();
                List&lt;DemoDto&gt; demoDtos = new ArrayList&lt;&gt;();
				while (rs.next()) {
                     DemoDto demoDto = new DemoDto();
                     demoDto.setDemoValueOne(rs.getString(&quot;Column_name_here&quot;));
                     demoDtos.add(demoDto);
                       // Get the values from ResultSet in this case rs
                }
                //Have an impelementation Taking care of this
                //rs.close(); 
                //ps.close();
            }

Hope this get helpful to someone in the future, it really was a pain in a spin.
Be careful when paginating though, its really flexible.

huangapple
  • 本文由 发表于 2020年7月23日 18:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63052382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定