Skip to content

Hibernate 5 Multitenancy tutorial#1150

Merged
KevinGilmore merged 13 commits into
eugenp:masterfrom
joshiparthin:hibernate5-multitenancy
Apr 20, 2017
Merged

Hibernate 5 Multitenancy tutorial#1150
KevinGilmore merged 13 commits into
eugenp:masterfrom
joshiparthin:hibernate5-multitenancy

Conversation

@joshiparthin

Copy link
Copy Markdown
Contributor

No description provided.

@KevinGilmore KevinGilmore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hibernate5/pom.xml
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread hibernate5/pom.xml Outdated
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest is 5.2.9.Final

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread hibernate5/pom.xml Outdated
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest is now 1.4.194

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread hibernate5/pom.xml Outdated
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml/classmate -->
<dependency>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Its working.

* Suppliers generated by hbm2java
*/
@Entity(name = "Supplier")
@Table(name ="Suppliers")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make table name "Supplier" also

@KevinGilmore KevinGilmore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve merge conflicts

Comment thread pom.xml
<module>hibernate5</module>
</modules>

</project>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 KevinGilmore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See individual files for details.

Comment thread hibernate5/pom.xml Outdated
<version>4.12</version>
</dependency>

<dependency>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the mysql library?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevinGilmore Done. Removed.

Session currentSession = sessionFactory.withOptions().tenantIdentifier("h2db1").openSession();
Transaction transaction = currentSession.getTransaction();
transaction.begin();
// currentSession.createSQLQuery("DROP ALL OBJECTS;").executeUpdate();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we don't need this line, let's remove it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use "Supplier" for the POJO and "supplier" for the table name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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> -->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the commented-out mysql lines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<!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"/>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supplier

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 KevinGilmore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the createCriteria method is deprecated

System.out.println("Init DB1");
Transaction transaction = db1Session.getTransaction();
transaction.begin();
db1Session.createSQLQuery("DROP ALL OBJECTS").executeUpdate();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the createSQLQuery method is deprecated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for the explanation.

@@ -0,0 +1,38 @@
package com.baeldung.hibernate;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,73 @@
package com.baeldung.hibernate;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this class to src/test/java

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) {
MetadataSources metadataSources = new MetadataSources( serviceRegistry );
for(Class annotatedClasses : getAnnotatedClasses()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not fluent in Java 8 streaming API, but if there is way to leverage it here, let's do so.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about it, but it may come up again in a higher-level review.

@KevinGilmore KevinGilmore merged commit 76673a3 into eugenp:master Apr 20, 2017
Comment thread pom.xml
<configuration>
<executable>maven</executable>

<module>hibernate5</module>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevinGilmore @joshiparthin Why is this here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants