Skip to content

Commit 028bf47

Browse files
MohitMohit
Mohit
authored and
Mohit
committedNov 4, 2024
Example for Hibernate
1 parent 5a5c1dc commit 028bf47

File tree

9 files changed

+180
-7
lines changed

9 files changed

+180
-7
lines changed
 

‎examples/build.gradle

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ dependencies {
2828
implementation project(':core')
2929
implementation project(':runtime')
3030
implementation project(':guice')
31+
implementation project(':contrib:hibernate')
3132

3233
// In actual
33-
// implementation "com.flipkart.grpc-jexpress:core:${jexpressVersion}"
34-
// implementation "com.flipkart.grpc-jexpress:runtime:${jexpressVersion}"
35-
// implementation "com.flipkart.grpc-jexpress:guice:${jexpressVersion}"
34+
// implementation "com.flipkart.grpc-jexpress:core:${jexpressVersion}"
35+
// implementation "com.flipkart.grpc-jexpress:runtime:${jexpressVersion}"
36+
// implementation "com.flipkart.grpc-jexpress:guice:${jexpressVersion}"
37+
// implementation "com.flipkart.grpc-jexpress:hibernate:${jexpressVersion}"
3638

3739
implementation "io.grpc:grpc-netty-shaded:${grpcVersion}"
3840
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
@@ -52,6 +54,7 @@ dependencies {
5254
implementation 'org.glassfish.jersey.containers:jersey-container-servlet:2.6'
5355
implementation 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.7'
5456
implementation 'javax.servlet:javax.servlet-api:3.1.0'
57+
implementation 'org.hibernate:hibernate-core:5.6.15.Final'
5558
}
5659

5760
protobuf {

‎examples/src/main/java/com/flipkart/gjex/examples/helloworld/HelloWorldApplication.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616
package com.flipkart.gjex.examples.helloworld;
1717

1818
import com.flipkart.gjex.core.Application;
19+
import com.flipkart.gjex.core.GJEXConfiguration;
20+
import com.flipkart.gjex.core.filter.grpc.GrpcFilter;
21+
import com.flipkart.gjex.core.filter.http.HttpFilter;
22+
import com.flipkart.gjex.core.job.ScheduledJob;
1923
import com.flipkart.gjex.core.setup.Bootstrap;
2024
import com.flipkart.gjex.core.setup.Environment;
25+
import com.flipkart.gjex.db.PooledDataSourceFactory;
2126
import com.flipkart.gjex.examples.helloworld.config.HelloWorldConfiguration;
2227
import com.flipkart.gjex.examples.helloworld.guice.HelloWorldModule;
2328
import com.flipkart.gjex.guice.GuiceBundle;
29+
import com.flipkart.gjex.hibernate.ScanningHibernateBundle;
2430

31+
import java.util.Collections;
32+
import java.util.List;
2533
import java.util.Map;
2634

2735
/**
@@ -39,9 +47,40 @@ public String getName() {
3947

4048
@Override
4149
public void initialize(Bootstrap<HelloWorldConfiguration, Map> bootstrap) {
50+
51+
ScanningHibernateBundle<HelloWorldConfiguration, Map> hibernateBundle =
52+
new ScanningHibernateBundle<HelloWorldConfiguration, Map>("entity") {
53+
@Override
54+
public PooledDataSourceFactory getDataSourceFactory(GJEXConfiguration var1) {
55+
HelloWorldConfiguration configuration = (HelloWorldConfiguration) var1;
56+
return configuration.getDataSourceFactory();
57+
}
58+
59+
@Override
60+
public List<GrpcFilter> getGrpcFilters() {
61+
return Collections.emptyList();
62+
}
63+
64+
@Override
65+
public List<HttpFilter> getHTTPFilters() {
66+
return Collections.emptyList();
67+
}
68+
69+
@Override
70+
public List getHealthChecks() {
71+
return Collections.emptyList();
72+
}
73+
74+
@Override
75+
public List<ScheduledJob> getScheduledJobs() {
76+
return Collections.emptyList();
77+
}
78+
};
79+
80+
bootstrap.addBundle(hibernateBundle);
4281
GuiceBundle<HelloWorldConfiguration, Map> guiceBundle = new GuiceBundle.Builder<HelloWorldConfiguration, Map>()
4382
.setConfigClass(HelloWorldConfiguration.class)
44-
.addModules(new HelloWorldModule())
83+
.addModules(new HelloWorldModule(hibernateBundle.getSessionFactory()))
4584
.build();
4685
bootstrap.addBundle(guiceBundle);
4786
}

‎examples/src/main/java/com/flipkart/gjex/examples/helloworld/config/HelloWorldConfiguration.java

+6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import com.fasterxml.jackson.annotation.JsonProperty;
77
import com.flipkart.gjex.core.GJEXConfiguration;
88

9+
import com.flipkart.gjex.db.DataSourceFactory;
910
import lombok.Data;
1011
import lombok.EqualsAndHashCode;
1112

13+
import javax.validation.Valid;
14+
1215

1316
@Data
1417
@EqualsAndHashCode(callSuper=true)
@@ -21,4 +24,7 @@ public class HelloWorldConfiguration extends GJEXConfiguration {
2124
@JsonProperty("task.properties")
2225
private Map<String, Object> taskProperties;
2326

27+
@Valid
28+
@JsonProperty("database")
29+
private DataSourceFactory dataSourceFactory;
2430
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.flipkart.gjex.examples.helloworld.dao;
2+
3+
import com.flipkart.gjex.examples.helloworld.entity.DummyEntity;
4+
import com.flipkart.gjex.hibernate.AbstractDAO;
5+
import org.hibernate.SessionFactory;
6+
7+
import javax.inject.Inject;
8+
9+
10+
public class DummyDAO extends AbstractDAO<DummyEntity> {
11+
12+
@Inject
13+
public DummyDAO(SessionFactory sessionFactory) {
14+
super(sessionFactory);
15+
}
16+
17+
public DummyEntity findById(Long id) {
18+
return get(id);
19+
}
20+
21+
public DummyEntity create(DummyEntity person) {
22+
return persist(person);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.flipkart.gjex.examples.helloworld.entity;
2+
3+
import com.google.type.DateTime;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
import javax.persistence.Table;
10+
11+
@Entity
12+
@Table(name = "dummyEntity")
13+
public class DummyEntity {
14+
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.IDENTITY)
17+
private Long id;
18+
private DateTime dateTime;
19+
private String status;
20+
}

‎examples/src/main/java/com/flipkart/gjex/examples/helloworld/guice/HelloWorldModule.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@
2626
import com.flipkart.gjex.examples.helloworld.tracing.AllWhitelistTracingSampler;
2727
import com.flipkart.gjex.examples.helloworld.web.HelloWorldResourceConfig;
2828
import com.flipkart.gjex.examples.helloworld.web.javaxfilter.ExampleJavaxFilter;
29+
import com.flipkart.gjex.hibernate.UnitOfWork;
30+
import com.flipkart.gjex.hibernate.UnitOfWorkInterceptor;
2931
import com.google.inject.AbstractModule;
32+
import com.google.inject.Provides;
3033
import com.google.inject.name.Names;
3134
import io.grpc.BindableService;
3235
import io.grpc.ManagedChannel;
3336
import io.grpc.ManagedChannelBuilder;
3437
import io.grpc.examples.helloworld.GreeterGrpc;
3538
import org.glassfish.jersey.server.ResourceConfig;
39+
import org.hibernate.SessionFactory;
40+
41+
import javax.inject.Singleton;
42+
43+
import static com.google.inject.matcher.Matchers.annotatedWith;
44+
import static com.google.inject.matcher.Matchers.any;
3645

3746
/**
3847
* Guice module for wiring sample Service to GJEX runtime
@@ -41,7 +50,10 @@
4150
*/
4251
public class HelloWorldModule extends AbstractModule {
4352

44-
public HelloWorldModule() {}
53+
private final SessionFactory sessionFactory;
54+
public HelloWorldModule(SessionFactory sessionFactory) {
55+
this.sessionFactory = sessionFactory;
56+
}
4557

4658
@Override
4759
protected void configure() {
@@ -57,5 +69,15 @@ protected void configure() {
5769
bind(ResourceConfig.class).annotatedWith(Names.named("HelloWorldResourceConfig")).to(HelloWorldResourceConfig.class);
5870
bind(JavaxFilterParams.class).annotatedWith(Names.named("ExampleJavaxFilter")).toInstance(JavaxFilterParams.builder().filter(new ExampleJavaxFilter()).pathSpec("/*").build());
5971
bind(HttpFilterParams.class).annotatedWith(Names.named("CustomHeaderHttpFilter")).toInstance(HttpFilterParams.builder().filter(new CustomHeaderHttpFilter()).pathSpec("/*").build());
60-
}
72+
73+
UnitOfWorkInterceptor unitOfWorkInterceptor = new UnitOfWorkInterceptor();
74+
requestInjection(unitOfWorkInterceptor);
75+
bindInterceptor(any(), annotatedWith(UnitOfWork.class), unitOfWorkInterceptor);
76+
}
77+
78+
@Provides
79+
@Singleton
80+
public SessionFactory getSessionFactory() {
81+
return sessionFactory;
82+
}
6183
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.flipkart.gjex.examples.helloworld.web;
2+
3+
import com.flipkart.gjex.examples.helloworld.dao.DummyDAO;
4+
import com.flipkart.gjex.examples.helloworld.entity.DummyEntity;
5+
6+
import javax.inject.Inject;
7+
import javax.inject.Named;
8+
import javax.inject.Singleton;
9+
import javax.ws.rs.GET;
10+
import javax.ws.rs.POST;
11+
import javax.ws.rs.Path;
12+
import javax.ws.rs.PathParam;
13+
import javax.ws.rs.core.Response;
14+
import java.util.Optional;
15+
16+
@Singleton
17+
@Path("/hibernate/example")
18+
@Named
19+
public class ExampleHibernateResource {
20+
21+
private final DummyDAO dummyDAO;
22+
23+
@Inject
24+
public ExampleHibernateResource(DummyDAO dummyDAO) {
25+
this.dummyDAO = dummyDAO;
26+
}
27+
28+
@POST
29+
public Response createPerson(DummyEntity person) {
30+
DummyEntity createdPerson = dummyDAO.create(person);
31+
return Response.status(Response.Status.CREATED).entity(createdPerson).build();
32+
}
33+
34+
@GET
35+
@Path("/{id}")
36+
public Response getPersonById(@PathParam("id") Long id) {
37+
Optional<DummyEntity> person = Optional.ofNullable(dummyDAO.findById(id));
38+
if (person.isPresent()) {
39+
return Response.ok(person.get()).build();
40+
} else {
41+
return Response.status(Response.Status.NOT_FOUND).build();
42+
}
43+
}
44+
}

‎examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/HelloWorldResourceConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public class HelloWorldResourceConfig extends ResourceConfig {
3232

3333
@Inject
3434
public HelloWorldResourceConfig (HelloWorldResource1 helloWorldresource1,
35-
HelloWorldResource2 helloWorldresource2) {
35+
HelloWorldResource2 helloWorldresource2, ExampleHibernateResource hibernateResource) {
3636
register(helloWorldresource1);
3737
register(helloWorldresource2);
38+
register(hibernateResource);
3839
}
3940

4041
}

‎examples/src/main/resources/hello_world_config.yml

+14
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,17 @@ apiProperties:
3737

3838
taskProperties:
3939
hello.timeout: 200
40+
41+
database:
42+
driverClass: com.mysql.jdbc.Driver
43+
user: new_user
44+
password: new_password
45+
url: jdbc:mysql://127.0.0.1:3306/overlord?autoReconnect=true&useSSL=false
46+
properties:
47+
charSet: UTF-8
48+
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
49+
hibernate.show_sql: true
50+
hibernate.current_session_context_class: managed
51+
checkConnectionWhileIdle: true
52+
checkConnectionOnReturn: true
53+
checkConnectionOnBorrow: true

0 commit comments

Comments
 (0)
Please sign in to comment.