首页 \ 问答 \ 如何在Spring数据JPA中使用XML配置进行命名查询?(How to use XML configuration for named queries in Spring data JPA?)

如何在Spring数据JPA中使用XML配置进行命名查询?(How to use XML configuration for named queries in Spring data JPA?)

根据文档,我们可以在XML中指定命名查询。 但它没有告诉如何在代码中使用它。 有人可以帮忙吗?


As per the Documentation we can specify named queries in an XML. But it does not tell how to use it in the code. Can someone please help?


原文:https://stackoverflow.com/questions/24931248
更新时间:2022-08-05 09:08

最满意答案

解决了

这是我的MongoUserDetailsS​​ervice中的冲突。

这是一个运作良好的新版本!(OBS!新登录是用电子邮件)

@Component
public class MongoUserDetailsService implements UserDetailsService
{
    public MongoOperations mongoOperations;

    private User userDetails;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException
    {

        boolean enabled = true;
        boolean accountNonLocked = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        try
        {
            mongoOperations = new MongoTemplate(new MongoClient(), "booking");
            Customer user = getUserByEmail(email);

            userDetails = new User(user.getEmail(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,
                    getAuthorities(user.getRoleAsInt()));
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }


        return userDetails;
    }

    public List getAuthorities(Integer role)
    {

        List authList = new ArrayList();
        if (role.intValue() == 2)
        {
            authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        if (role.intValue() == 1)
        {
            authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        }

        return authList;

    }

    public Customer getUserByEmail(String email)
    {
        Query query = new Query();
        query.addCriteria(Criteria.where("email").is(email));
        Customer customer = mongoOperations.findOne(query, Customer.class);

        return customer;
    }

}


Solved

It was a conflict in my MongoUserDetailsService.

Here is the new one which is working well!(OBS! The new log in is with email)

@Component
public class MongoUserDetailsService implements UserDetailsService
{
    public MongoOperations mongoOperations;

    private User userDetails;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException
    {

        boolean enabled = true;
        boolean accountNonLocked = true;
        boolean accountNonExpired = true;
        boolean credentialsNonExpired = true;
        try
        {
            mongoOperations = new MongoTemplate(new MongoClient(), "booking");
            Customer user = getUserByEmail(email);

            userDetails = new User(user.getEmail(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,
                    getAuthorities(user.getRoleAsInt()));
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }


        return userDetails;
    }

    public List getAuthorities(Integer role)
    {

        List authList = new ArrayList();
        if (role.intValue() == 2)
        {
            authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        if (role.intValue() == 1)
        {
            authList.add(new SimpleGrantedAuthority("ROLE_USER"));
        }

        return authList;

    }

    public Customer getUserByEmail(String email)
    {
        Query query = new Query();
        query.addCriteria(Criteria.where("email").is(email));
        Customer customer = mongoOperations.findOne(query, Customer.class);

        return customer;
    }

}

相关问答

更多
  • 规范的方法是在没有参数的init()方法中使用继承的GenericServlet#getInitParameter() (并删除任何init(config)方法)。 @Override public void init() throws ServletException { filename = getInitParameter("addressfile"); } 如果仍然不起作用,那么你的web.xml没有被正确地部署,或者你在参数名称中有一个错字,或者你实际上访问了一个不同于filename实 ...
  • 原因之一 - 在MainFrame中调用getStudentsPass()时,尚未设置密码。 原因二 - 您在MainFrame中使用的SystemManagement对象和您在AddStudent中获得的SystemManagement对象完全不一样。 很难说没有更多的代码。 但我更喜欢Reason One - 检查程序流程,密码何时设置,什么时候尝试获取。 Reason one - password has not been set, when you call getStudentsPass() in ...
  • 因为例外rwe不包含消息。 例如,这通常是NullPointerException的情况。 除了消息之外,您可能还希望包含异常类型(如果有消息)。 Because the exception rwe doesn't include a message. This is typically the case with a NullPointerException, for instance. You might want to include the exception type in addition to ...
  • 会发生的是,在方法execute_var_to_set_e_get1和execute_var_to_set_e_get2每次调用中,您都在创建不同的对象。 在第一个中,您将name设置为String ,但在最后一个中,您没有这样做。 因此,它将为null (默认情况下)。 注意:您描述的行为属于static属性。 如果将name为static ,它将在所有实例之间共享。 What happens is that in each call of the methods execute_var_to_set_e ...
  • 正如@Rajendra Gujja在评论中提到的那样,“将密钥保存在地图后,密钥的哈希码不应该改变”。 这是非常正确的; 一旦我将所有的哈希码更改为简单地使用UUID而不是更改的name属性,问题就解决了。 谢谢你的所有答案! As @Rajendra Gujja mentioned in a comment, the "hashcode of your keys should not change after you keep them in the map". This is very true; on ...
  • 首先,您需要上下文的类加载器。 其次,你需要更换点. 在classname中使用正斜杠/并在.class扩展名后缀以标识真实路径。 所以这个应该工作: String name = String.class.getName().replace(".", "/") + ".class"; URL url = Thread.currentThread().getContextClassLoader().getResource(name); InputStream input = Thread.currentThr ...
  • 你应该使用sudo -E和export来保留环境变量: $ export TestEnviron=varproperty $ sudo -E java -jar JavaEnvironmentVariable.jar 但是为了更安全,您应该配置sudo以保持您的变量,如本文所述: 如何在使用SUDO时保留环境变量 : sudo visudo 并添加以下行: Defaults env_keep += "TestEnviron" You should use sudo -E AND export to k ...
  • 想想你为什么期望u.getUsername()为非null。 首先,您要创建一个用户实例: User u = new User(); 然后为某些本地username和password变量分配一些值: String username; String password; System.out.println("Enter Username: "); username = input.next(); System.out.println("Enter Password: "); password = inp ...
  • 解决了 这是我的MongoUserDetailsService中的冲突。 这是一个运作良好的新版本!(OBS!新登录是用电子邮件) @Component public class MongoUserDetailsService implements UserDetailsService { public MongoOperations mongoOperations; private User userDetails; @Override public UserDetai ...
  • 尝试这个: // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(config, "tablename"); // Instantiating the Scan class Scan scan = new Scan(); // Scanning ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)