Hibernate 5 Multitenancy tutorial#1150
Conversation
KevinGilmore
left a comment
There was a problem hiding this comment.
There are merge conflicts -- you'll need to pull in the latest changes from the main repo. Also I made a few notes in the pom.xml.
| <version>6.0.5</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.h2database</groupId> |
There was a problem hiding this comment.
Adjust the indentation here -- looks like this one uses spaces and the others use tabs. Spaces are preferred, but the main point is to be consistent.
| <dependency> | ||
| <groupId>org.hibernate</groupId> | ||
| <artifactId>hibernate-core</artifactId> | ||
| <version>5.2.6.Final</version> |
| <dependency> | ||
| <groupId>com.h2database</groupId> | ||
| <artifactId>h2</artifactId> | ||
| <version>1.4.193</version> |
| <version>4.12</version> | ||
| </dependency> | ||
| <!-- https://mvnrepository.com/artifact/com.fasterxml/classmate --> | ||
| <dependency> |
There was a problem hiding this comment.
Are you certain that all of these other dependencies are needed in order to use hibernate? Are they not brought in as transitive properties when you include hibernate-core in your POM? Let me know what you find.
There was a problem hiding this comment.
Removed. Its working.
| * Suppliers generated by hbm2java | ||
| */ | ||
| @Entity(name = "Supplier") | ||
| @Table(name ="Suppliers") |
There was a problem hiding this comment.
Make table name "Supplier" also
KevinGilmore
left a comment
There was a problem hiding this comment.
Resolve merge conflicts
| <module>hibernate5</module> | ||
| </modules> | ||
|
|
||
| </project> |
There was a problem hiding this comment.
There's a merge conflict with this file -- you'll need to update your branch from upstream, resolve the conflict(s), and do another push. Not sure what the problem is exactly -- could be as simple as restoring the empty line at the end of the file.
There was a problem hiding this comment.
@KevinGilmore I think this is about parent pom.xml file. I've pulled, merged, resolved, commited and pushed. Let me know still if you are facing issues.
KevinGilmore
left a comment
There was a problem hiding this comment.
See individual files for details.
| <version>4.12</version> | ||
| </dependency> | ||
|
|
||
| <dependency> |
There was a problem hiding this comment.
Do we still need the mysql library?
| Session currentSession = sessionFactory.withOptions().tenantIdentifier("h2db1").openSession(); | ||
| Transaction transaction = currentSession.getTransaction(); | ||
| transaction.begin(); | ||
| // currentSession.createSQLQuery("DROP ALL OBJECTS;").executeUpdate(); |
There was a problem hiding this comment.
if we don't need this line, let's remove it
There was a problem hiding this comment.
@KevinGilmore This class is not used in article. I have just taken here for testing the boostrapping of multitenancy. Let me know if you want me to delete it.
| "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> | ||
| <!-- Generated Feb 9, 2017 11:31:36 AM by Hibernate Tools 5.1.0.Final --> | ||
| <hibernate-mapping> | ||
| <class name="com.baeldung.hibernate.pojo.Suppliers" table="suppliers" optimistic-lock="version"> |
There was a problem hiding this comment.
Shouldn't this use "Supplier" for the POJO and "supplier" for the table name?
There was a problem hiding this comment.
@KevinGilmore We have used entity classes mapping so this hbm is no more required. Earlier I tried it with hbm and thus this code was from that trial. We dont need this anymore and have removed in today's commits.
| "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> | ||
| <hibernate-configuration> | ||
| <session-factory> | ||
| <!--<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> --> |
There was a problem hiding this comment.
We can remove the commented-out mysql lines.
| <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" > | ||
|
|
||
| <hibernate-reverse-engineering> | ||
| <table-filter match-catalog="db1" match-name="suppliers"/> |
There was a problem hiding this comment.
@KevinGilmore as commented for hbm we dont need this file anymore and hence removed.
hibernate5-multitenancy Conflicts: pom.xml Also code clean up done.
KevinGilmore
left a comment
There was a problem hiding this comment.
Please see comments in individual files for details.
| Transaction transaction = currentSession.getTransaction(); | ||
| transaction.begin(); | ||
| // currentSession.createSQLQuery("DROP ALL OBJECTS;").executeUpdate(); | ||
| currentSession.createCriteria(Suppliers.class).list().stream().forEach(System.out::println); |
There was a problem hiding this comment.
the createCriteria method is deprecated
| System.out.println("Init DB1"); | ||
| Transaction transaction = db1Session.getTransaction(); | ||
| transaction.begin(); | ||
| db1Session.createSQLQuery("DROP ALL OBJECTS").executeUpdate(); |
There was a problem hiding this comment.
the createSQLQuery method is deprecated
There was a problem hiding this comment.
@KevinGilmore well since we are using legacy config method (SessionManager) for boostrapping against the EntityManager, we will have to use this functions though deprecated as of now for multitenancy example. As there is no documentation of how to do that with entitymanager bootstrapping method.
There was a problem hiding this comment.
OK, thanks for the explanation.
| @@ -0,0 +1,38 @@ | |||
| package com.baeldung.hibernate; | |||
| @@ -0,0 +1,73 @@ | |||
| package com.baeldung.hibernate; | |||
There was a problem hiding this comment.
Let's move this class to src/test/java
|
|
||
| private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) { | ||
| MetadataSources metadataSources = new MetadataSources( serviceRegistry ); | ||
| for(Class annotatedClasses : getAnnotatedClasses()) { |
There was a problem hiding this comment.
I'm not fluent in Java 8 streaming API, but if there is way to leverage it here, let's do so.
There was a problem hiding this comment.
@KevinGilmore we can do that but will have to convert array results given by getAnnotedClasses() to list and then use stream. So I thought to keep it as it is. What do you feel ?
There was a problem hiding this comment.
I don't feel strongly about it, but it may come up again in a higher-level review.
| <configuration> | ||
| <executable>maven</executable> | ||
|
|
||
| <module>hibernate5</module> |
No description provided.