Guest User

Untitled

a guest
Apr 24th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. An exception occurred while creating a query in EntityManager:
  2. Exception Description: Problem compiling [SELECT u.idutilisateur FROM Utilisateur u where u.nomutilisateur=:login and u.passwordutilisateur=:password].
  3. [7, 22] The state field path 'u.idutilisateur' cannot be resolved to a valid type.
  4. [48, 64] The state field path 'u.nomutilisateur' cannot be resolved to a valid type.
  5. [78, 99] The state field path 'u.passwordutilisateur' cannot be resolved to a valid type.
  6.  
  7. public class MultiTenantJpaTransactionManager extends JpaTransactionManager {
  8.  
  9. /**
  10. *
  11. */
  12. private static final long serialVersionUID = 1L;
  13. private static EntityManagerFactory entityManagerFactory;
  14. static Map<Object, Object> properties = new HashMap<>();
  15.  
  16.  
  17. /**
  18. * This process shouldn't be interrupted by concurrent thread. Therefore the
  19. * method is synchronized.
  20. */
  21. @Override
  22. public Map<String, Object> getJpaPropertyMap() {
  23. super.getJpaPropertyMap()
  24. .put(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT,
  25. "tenant-id");
  26. return super.getJpaPropertyMap();
  27. }
  28. /**
  29. * This process shouldn't be interrupted by concurrent thread. Therefore the
  30. * method is synchronized.
  31. */
  32. @Override
  33. protected synchronized EntityManager createEntityManagerForTransaction() {
  34. EntityManager em = null;
  35. Map<String, Object> properties = getJpaPropertyMap();
  36. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
  37. .getRequest();
  38. URL url = null;
  39. String host = null;
  40. // get EMF from JpaTransactionManager
  41. EntityManagerFactory emf = ((EntityManagerFactoryInfo) getEntityManagerFactory())
  42. .getNativeEntityManagerFactory();
  43. boolean isMetadataExpired = ((EntityManagerFactoryImpl) emf).unwrap().getSetupImpl().isMetadataExpired();
  44. // it is needed to get EM to update metadata if they are marked as
  45. // expired otherwise serverSesstion and
  46. // therefore also actualTenant value being get below wouldn't be actual
  47. if (isMetadataExpired) {
  48. em = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties)
  49. : emf.createEntityManager());
  50. }
  51. Server ss = JpaHelper.getServerSession(emf);
  52. String actualTenant = (String) ss.getProperty("tenant-id");
  53. // don't run it if tenant didn't change
  54. // it should be quite faster then
  55. try {
  56. url = new URL(request.getRequestURL().toString());
  57. } catch (MalformedURLException e) {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. host = url.getHost();
  62. if ((actualTenant == null)
  63. || (actualTenant != null && !actualTenant.equals(getHost(host) != null))) {
  64. Map<String,Object> newProperties = new HashMap<String,Object>();
  65. newProperties.put("tenant-id",
  66. getHost(host));
  67. newProperties.put("eclipselink.session-name", "multi-tenant-" + getHost(host));
  68. newProperties.put(PersistenceUnitProperties.MULTITENANT_SHARED_CACHE, "false");
  69. newProperties.put(PersistenceUnitProperties.MULTITENANT_SHARED_EMF, "true");
  70. JpaHelper.getEntityManagerFactory(emf).refreshMetadata(newProperties);
  71. } else if (em != null) {
  72. // don't get it again
  73. // it is unnecessary
  74. return em;
  75. }
  76.  
  77. EntityManager ret = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
  78. ret.setProperty("tenant-id", getHost(host));
  79. return ret;
  80.  
  81. }
  82.  
  83. private static String getHost(String host) {
  84. int i = host.indexOf('.');
  85. String hostName = host.substring(0, i);
  86. System.out.println("calculated hostname: " + hostName);
  87. return "tenant1";
  88. }
  89. }
Add Comment
Please, Sign In to add comment