首页
\
问答
\
如何在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
最满意答案
解决了
这是我的MongoUserDetailsService中的冲突。
这是一个运作良好的新版本!(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实 ...
-
为什么它返回null?(why it returns null?)[2023-07-23]
原因之一 - 在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 ...
-
Java的。(Java. Get/Set returns null)[2023-02-07]
会发生的是,在方法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 ... -
Java映射值返回null(Java map value returns null)[2022-03-08]
正如@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 ...
-
Java getenv()返回null(Java getenv() returns null)[2023-12-07]
你应该使用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 ...
-
java config中的MongoUserDetailsService返回null(MongoUserDetailsService in java config returns null)[2023-03-24]
解决了 这是我的MongoUserDetailsService中的冲突。 这是一个运作良好的新版本!(OBS!新登录是用电子邮件) @Component public class MongoUserDetailsService implements UserDetailsService { public MongoOperations mongoOperations; private User userDetails; @Override public UserDetai ... -
HBase读取数据返回null(HBase read data returns null)[2022-03-31]
尝试这个: // 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 ...