Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- An exception occurred while creating a query in EntityManager:
- Exception Description: Problem compiling [SELECT u.idutilisateur FROM Utilisateur u where u.nomutilisateur=:login and u.passwordutilisateur=:password].
- [7, 22] The state field path 'u.idutilisateur' cannot be resolved to a valid type.
- [48, 64] The state field path 'u.nomutilisateur' cannot be resolved to a valid type.
- [78, 99] The state field path 'u.passwordutilisateur' cannot be resolved to a valid type.
- public class MultiTenantJpaTransactionManager extends JpaTransactionManager {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private static EntityManagerFactory entityManagerFactory;
- static Map<Object, Object> properties = new HashMap<>();
- /**
- * This process shouldn't be interrupted by concurrent thread. Therefore the
- * method is synchronized.
- */
- @Override
- public Map<String, Object> getJpaPropertyMap() {
- super.getJpaPropertyMap()
- .put(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT,
- "tenant-id");
- return super.getJpaPropertyMap();
- }
- /**
- * This process shouldn't be interrupted by concurrent thread. Therefore the
- * method is synchronized.
- */
- @Override
- protected synchronized EntityManager createEntityManagerForTransaction() {
- EntityManager em = null;
- Map<String, Object> properties = getJpaPropertyMap();
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- .getRequest();
- URL url = null;
- String host = null;
- // get EMF from JpaTransactionManager
- EntityManagerFactory emf = ((EntityManagerFactoryInfo) getEntityManagerFactory())
- .getNativeEntityManagerFactory();
- boolean isMetadataExpired = ((EntityManagerFactoryImpl) emf).unwrap().getSetupImpl().isMetadataExpired();
- // it is needed to get EM to update metadata if they are marked as
- // expired otherwise serverSesstion and
- // therefore also actualTenant value being get below wouldn't be actual
- if (isMetadataExpired) {
- em = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties)
- : emf.createEntityManager());
- }
- Server ss = JpaHelper.getServerSession(emf);
- String actualTenant = (String) ss.getProperty("tenant-id");
- // don't run it if tenant didn't change
- // it should be quite faster then
- try {
- url = new URL(request.getRequestURL().toString());
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- host = url.getHost();
- if ((actualTenant == null)
- || (actualTenant != null && !actualTenant.equals(getHost(host) != null))) {
- Map<String,Object> newProperties = new HashMap<String,Object>();
- newProperties.put("tenant-id",
- getHost(host));
- newProperties.put("eclipselink.session-name", "multi-tenant-" + getHost(host));
- newProperties.put(PersistenceUnitProperties.MULTITENANT_SHARED_CACHE, "false");
- newProperties.put(PersistenceUnitProperties.MULTITENANT_SHARED_EMF, "true");
- JpaHelper.getEntityManagerFactory(emf).refreshMetadata(newProperties);
- } else if (em != null) {
- // don't get it again
- // it is unnecessary
- return em;
- }
- EntityManager ret = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
- ret.setProperty("tenant-id", getHost(host));
- return ret;
- }
- private static String getHost(String host) {
- int i = host.indexOf('.');
- String hostName = host.substring(0, i);
- System.out.println("calculated hostname: " + hostName);
- return "tenant1";
- }
- }
Add Comment
Please, Sign In to add comment