gpt4 book ai didi

hibernate - 如何在 Grails 中调用命名查询

转载 作者:行者123 更新时间:2023-12-02 13:54:30 25 4
gpt4 key购买 nike

鉴于以下 example from Hibernate Make Easy ,如何在 Grails 中调用命名查询 user.findByLoginName?

package com.examscam.model;
import javax.persistence.*;
import org.hibernate.Session;
import com.examscam.HibernateUtil;
@Entity
@Table(name = "user", schema = "examscam")
@NamedQuery(name="user.findByLoginName",
query="from User where loginName = :name" )
public class User { }

最佳答案

您必须使用 hibernate session 。

在 Grails 1.1 中,您可以使用该类的 withSession 方法。

User.withSession { session ->
return session.getNamedQuery('user.findByLoginName')
.setString('name', 'someName')
.list()
}

使用 Grails 1.0,您需要将 sessionFactory 对象注入(inject)到您的 Controller/Service 中才能访问它。

关于hibernate - 如何在 Grails 中调用命名查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1102096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com