Skip to content

Commit 5b03025

Browse files
joshiparthinKevinGilmore
authored andcommitted
Added DAO for Supplier (eugenp#1950)
* First commit for Hibernate 5 Multitenancy tutorial * Changes to fix the code. * Added hibernate begin transaction code. * Changes to solve the multitenancy issue. * Changes to integrate h2 * Changing configs to solve the error * Changes to solve h2 error... * Changes to fix H2 error. * Cleaned POM.xml and changed entity name * Changes table name to supplier * Removed MySql Dep from pom.xml. * Changes as per comment in the PR... * Removed try-catch from test functions. * Removing the hibernate xml config. * Changed the formatting as per the formatter. * Clean up after merge. * # WARNING: head commit changed in the meantime Merge remote-tracking branch 'upstream/master' into hibernate5-multitenancy Conflicts: hibernate5/pom.xml pom.xml * Introduced Supplier DAO * shifting hibernate properties to src/test/resouces * removed the old test. * Filled up the stubs in the DAO.
1 parent 25de280 commit 5b03025

6 files changed

Lines changed: 123 additions & 80 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.hibernate.dao;
2+
3+
import java.util.List;
4+
5+
public interface GenericDao<T> {
6+
7+
void save (T entity);
8+
void delete (T Entity);
9+
T findByName(String name);
10+
List<T> findAll();
11+
void populate();
12+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.hibernate.dao;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.hibernate.Criteria;
7+
import org.hibernate.Session;
8+
import org.hibernate.SessionFactory;
9+
import org.hibernate.Transaction;
10+
import org.hibernate.criterion.Expression;
11+
12+
import com.baeldung.hibernate.pojo.Supplier;
13+
14+
public class SupplierDao implements GenericDao<Supplier>{
15+
private SessionFactory sessionFactory;
16+
private String tenant;
17+
18+
public SupplierDao(SessionFactory sessionFactory, String tenant) {
19+
this.sessionFactory = sessionFactory;
20+
this.tenant = tenant;
21+
populate();
22+
}
23+
24+
@Override
25+
public void save(Supplier entity) {
26+
Session session = sessionFactory.withOptions().tenantIdentifier(tenant).openSession();
27+
session.save(entity);
28+
}
29+
30+
@Override
31+
public void delete(Supplier supplier) {
32+
Session session = sessionFactory.withOptions().tenantIdentifier(tenant).openSession();
33+
session.delete(supplier);
34+
}
35+
36+
@Override
37+
public Supplier findByName(String name) {
38+
Session session = sessionFactory.withOptions().tenantIdentifier(tenant).openSession();
39+
List<Supplier> fetchedSuppliers = session.createCriteria(Supplier.class).add(Expression.eq("name", name)).list();
40+
if (fetchedSuppliers.size()>0) {
41+
return fetchedSuppliers.get(0);
42+
}else {
43+
return null;
44+
}
45+
}
46+
47+
@Override
48+
public List<Supplier> findAll() {
49+
Session session = sessionFactory.withOptions().tenantIdentifier(tenant).openSession();
50+
return session.createCriteria(Supplier.class).list();
51+
}
52+
53+
@Override
54+
public void populate() {
55+
System.out.println("Init DB1");
56+
Session session = sessionFactory.withOptions().tenantIdentifier(tenant).openSession();
57+
Transaction transaction = session.getTransaction();
58+
59+
transaction.begin();
60+
session.createSQLQuery("DROP ALL OBJECTS").executeUpdate();
61+
session
62+
.createSQLQuery(
63+
"create table Supplier (id integer generated by default as identity, country varchar(255), name varchar(255), primary key (id))")
64+
.executeUpdate();
65+
Supplier genertedSupplier = generateEntityForTenant(tenant);
66+
System.out.println("Inserting Supplier"+genertedSupplier);
67+
save (genertedSupplier);
68+
69+
}
70+
71+
private Supplier generateEntityForTenant(String forTenant) {
72+
if (forTenant.equals("mydb1")) {
73+
return new Supplier ("John","USA");
74+
}
75+
return new Supplier ("Miller","UK");
76+
}
77+
78+
79+
80+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.hibernate;
2+
3+
import java.io.IOException;
4+
5+
import org.hibernate.SessionFactory;
6+
import org.junit.Test;
7+
8+
import com.baeldung.hibernate.dao.SupplierDao;
9+
import com.baeldung.hibernate.pojo.Supplier;
10+
11+
import static org.junit.Assert.assertNull;;
12+
13+
public class MultiTenantDaoHibernateIntegrationTest {
14+
@Test
15+
public void givenDBMode_whenFetchingSuppliersByName_thenChecking() throws UnsupportedTenancyException, IOException {
16+
SessionFactory sessionFactory = HibernateMultiTenantUtil.getSessionFactory();
17+
18+
SupplierDao myDb1Dao = new SupplierDao(sessionFactory, "mydb1");
19+
Supplier db1SupplierName = myDb1Dao.findByName("John");
20+
21+
// finding the same supplier name in another tenant
22+
// and we should not be able to find in there and both dbs are different.
23+
SupplierDao myDb2Dao = new SupplierDao(sessionFactory, "mydb2");
24+
Supplier db2SupplierName = myDb2Dao.findByName(db1SupplierName.getName());
25+
26+
assertNull(db2SupplierName);
27+
28+
}
29+
30+
}

‎hibernate5/src/test/java/com/baeldung/hibernate/MultiTenantHibernateIntegrationTest.java‎

Lines changed: 0 additions & 72 deletions
This file was deleted.

‎hibernate5/src/test/java/hibernate.properties‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

hibernate5/src/main/resources/hibernate.properties renamed to hibernate5/src/test/resources/hibernate.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
hibernate.connection.driver_class=org.h2.Driver
22
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
33
hibernate.connection.username=sa
4+
hibernate.connection.autocommit=true
45
jdbc.password=
56

67
hibernate.dialect=org.hibernate.dialect.H2Dialect

0 commit comments

Comments
 (0)