Skip to content

Commit 90867eb

Browse files
authored
Merge pull request eugenp#5336 from josephine-barboza/master
BAEL-2169 Guide to Ebean
2 parents 19f5943 + af818fd commit 90867eb

9 files changed

Lines changed: 369 additions & 62 deletions

File tree

‎libraries-data/pom.xml‎

Lines changed: 106 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -148,42 +148,42 @@
148148
<version>${jmapper.version}</version>
149149
</dependency>
150150
<!-- crunch project -->
151-
<dependency>
152-
<groupId>org.apache.crunch</groupId>
153-
<artifactId>crunch-core</artifactId>
154-
<version>${org.apache.crunch.crunch-core.version}</version>
155-
</dependency>
156-
<dependency>
157-
<groupId>org.apache.hadoop</groupId>
158-
<artifactId>hadoop-client</artifactId>
159-
<version>${org.apache.hadoop.hadoop-client}</version>
160-
<scope>provided</scope>
161-
</dependency>
151+
<dependency>
152+
<groupId>org.apache.crunch</groupId>
153+
<artifactId>crunch-core</artifactId>
154+
<version>${org.apache.crunch.crunch-core.version}</version>
155+
</dependency>
156+
<dependency>
157+
<groupId>org.apache.hadoop</groupId>
158+
<artifactId>hadoop-client</artifactId>
159+
<version>${org.apache.hadoop.hadoop-client}</version>
160+
<scope>provided</scope>
161+
</dependency>
162162

163-
<dependency>
164-
<groupId>commons-cli</groupId>
165-
<artifactId>commons-cli</artifactId>
166-
<version>1.2</version>
167-
<scope>provided</scope>
168-
</dependency>
169-
<dependency>
170-
<groupId>commons-io</groupId>
171-
<artifactId>commons-io</artifactId>
172-
<version>2.1</version>
173-
<scope>provided</scope>
174-
</dependency>
175-
<dependency>
176-
<groupId>commons-httpclient</groupId>
177-
<artifactId>commons-httpclient</artifactId>
178-
<version>3.0.1</version>
179-
<scope>provided</scope>
180-
<exclusions>
181-
<exclusion>
182-
<groupId>commons-codec</groupId>
183-
<artifactId>commons-codec</artifactId>
184-
</exclusion>
185-
</exclusions>
186-
</dependency>
163+
<dependency>
164+
<groupId>commons-cli</groupId>
165+
<artifactId>commons-cli</artifactId>
166+
<version>1.2</version>
167+
<scope>provided</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>commons-io</groupId>
171+
<artifactId>commons-io</artifactId>
172+
<version>2.1</version>
173+
<scope>provided</scope>
174+
</dependency>
175+
<dependency>
176+
<groupId>commons-httpclient</groupId>
177+
<artifactId>commons-httpclient</artifactId>
178+
<version>3.0.1</version>
179+
<scope>provided</scope>
180+
<exclusions>
181+
<exclusion>
182+
<groupId>commons-codec</groupId>
183+
<artifactId>commons-codec</artifactId>
184+
</exclusion>
185+
</exclusions>
186+
</dependency>
187187
<dependency>
188188
<groupId>org.apache.flink</groupId>
189189
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
@@ -249,7 +249,32 @@
249249
<version>${awaitility.version}</version>
250250
<scope>test</scope>
251251
</dependency>
252+
<dependency>
253+
<groupId>io.ebean</groupId>
254+
<artifactId>ebean</artifactId>
255+
<version>${ebean.version}</version>
256+
</dependency>
257+
<dependency>
258+
<groupId>org.slf4j</groupId>
259+
<artifactId>slf4j-api</artifactId>
260+
<version>${slf4j.version}</version>
261+
</dependency>
252262

263+
<dependency>
264+
<groupId>ch.qos.logback</groupId>
265+
<artifactId>logback-classic</artifactId>
266+
<version>${logback.version}</version>
267+
</dependency>
268+
<dependency>
269+
<groupId>ch.qos.logback</groupId>
270+
<artifactId>logback-core</artifactId>
271+
<version>${logback.version}</version>
272+
</dependency>
273+
<dependency>
274+
<groupId>org.slf4j</groupId>
275+
<artifactId>slf4j-log4j12</artifactId>
276+
<version>${slf4j.version}</version>
277+
</dependency>
253278
</dependencies>
254279

255280
<build>
@@ -354,31 +379,48 @@
354379
</executions>
355380
</plugin>
356381

357-
<plugin>
358-
<groupId>org.apache.maven.plugins</groupId>
359-
<artifactId>maven-assembly-plugin</artifactId>
360-
<version>2.3</version>
361-
<configuration>
362-
<descriptors>
363-
<descriptor>src/main/assembly/hadoop-job.xml</descriptor>
364-
</descriptors>
365-
<archive>
366-
<manifest>
367-
<mainClass>com.baeldung.crunch.WordCount</mainClass>
368-
</manifest>
369-
</archive>
370-
</configuration>
371-
<executions>
372-
<execution>
373-
<id>make-assembly</id>
374-
<phase>package</phase>
375-
<goals>
376-
<goal>single</goal>
377-
</goals>
378-
</execution>
379-
</executions>
380-
</plugin>
381-
382+
<plugin>
383+
<groupId>org.apache.maven.plugins</groupId>
384+
<artifactId>maven-assembly-plugin</artifactId>
385+
<version>2.3</version>
386+
<configuration>
387+
<descriptors>
388+
<descriptor>src/main/assembly/hadoop-job.xml</descriptor>
389+
</descriptors>
390+
<archive>
391+
<manifest>
392+
<mainClass>com.baeldung.crunch.WordCount</mainClass>
393+
</manifest>
394+
</archive>
395+
</configuration>
396+
<executions>
397+
<execution>
398+
<id>make-assembly</id>
399+
<phase>package</phase>
400+
<goals>
401+
<goal>single</goal>
402+
</goals>
403+
</execution>
404+
</executions>
405+
</plugin>
406+
<plugin>
407+
<groupId>io.ebean</groupId>
408+
<artifactId>ebean-maven-plugin</artifactId>
409+
<version>11.11.2</version>
410+
<executions>
411+
<!-- enhance main classes -->
412+
<execution>
413+
<id>main</id>
414+
<phase>process-classes</phase>
415+
<configuration>
416+
<transformArgs>debug=1</transformArgs>
417+
</configuration>
418+
<goals>
419+
<goal>enhance</goal>
420+
</goals>
421+
</execution>
422+
</executions>
423+
</plugin>
382424
</plugins>
383425
</build>
384426

@@ -415,7 +457,10 @@
415457
<datanucleus-jdo-query.version>5.0.4</datanucleus-jdo-query.version>
416458
<jmapper.version>1.6.0.1</jmapper.version>
417459
<org.apache.crunch.crunch-core.version>0.15.0</org.apache.crunch.crunch-core.version>
418-
<org.apache.hadoop.hadoop-client>2.2.0</org.apache.hadoop.hadoop-client>
460+
<org.apache.hadoop.hadoop-client>2.2.0</org.apache.hadoop.hadoop-client>
461+
<ebean.version>11.22.4</ebean.version>
462+
<slf4j.version>1.7.25</slf4j.version>
463+
<logback.version>1.0.1</logback.version>
419464
</properties>
420465

421466
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.ebean.app;
2+
3+
import java.util.Arrays;
4+
5+
import com.baeldung.ebean.model.Address;
6+
import com.baeldung.ebean.model.Customer;
7+
8+
import io.ebean.Ebean;
9+
import io.ebean.EbeanServer;
10+
import io.ebean.annotation.Transactional;
11+
12+
public class App {
13+
14+
public static void main(String[] args) {
15+
insertAndDeleteInsideTransaction();
16+
crudOperations();
17+
queryCustomers();
18+
}
19+
20+
@Transactional
21+
public static void insertAndDeleteInsideTransaction() {
22+
23+
Customer c1 = getCustomer();
24+
EbeanServer server = Ebean.getDefaultServer();
25+
server.save(c1);
26+
Customer foundC1 = server.find(Customer.class, c1.getId());
27+
server.delete(foundC1);
28+
}
29+
30+
public static void crudOperations() {
31+
32+
Address a1 = new Address("5, Wide Street", null, "New York");
33+
Customer c1 = new Customer("John Wide", a1);
34+
35+
EbeanServer server = Ebean.getDefaultServer();
36+
server.save(c1);
37+
38+
c1.setName("Jane Wide");
39+
c1.setAddress(null);
40+
server.save(c1);
41+
42+
Customer foundC1 = Ebean.find(Customer.class, c1.getId());
43+
44+
Ebean.delete(foundC1);
45+
}
46+
47+
public static void queryCustomers() {
48+
Address a1 = new Address("1, Big Street", null, "New York");
49+
Customer c1 = new Customer("Big John", a1);
50+
51+
Address a2 = new Address("2, Big Street", null, "New York");
52+
Customer c2 = new Customer("Big John", a2);
53+
54+
Address a3 = new Address("3, Big Street", null, "San Jose");
55+
Customer c3 = new Customer("Big Bob", a3);
56+
57+
Ebean.saveAll(Arrays.asList(c1, c2, c3));
58+
59+
Customer customer = Ebean.find(Customer.class)
60+
.select("name")
61+
.fetch("address", "city")
62+
.where()
63+
.eq("city", "San Jose")
64+
.findOne();
65+
66+
Ebean.deleteAll(Arrays.asList(c1, c2, c3));
67+
}
68+
69+
private static Customer getCustomer() {
70+
Address a1 = new Address("1, Big Street", null, "New York");
71+
Customer c1 = new Customer("Big John", a1);
72+
return c1;
73+
}
74+
75+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.ebean.app;
2+
3+
import java.util.Properties;
4+
5+
import io.ebean.EbeanServer;
6+
import io.ebean.EbeanServerFactory;
7+
import io.ebean.config.ServerConfig;
8+
9+
public class App2 {
10+
11+
public static void main(String[] args) {
12+
ServerConfig cfg = new ServerConfig();
13+
cfg.setDefaultServer(true);
14+
Properties properties = new Properties();
15+
properties.put("ebean.db.ddl.generate", "true");
16+
properties.put("ebean.db.ddl.run", "true");
17+
properties.put("datasource.db.username", "sa");
18+
properties.put("datasource.db.password", "");
19+
properties.put("datasource.db.databaseUrl", "jdbc:h2:mem:app2");
20+
properties.put("datasource.db.databaseDriver", "org.h2.Driver");
21+
cfg.loadFromProperties(properties);
22+
EbeanServer server = EbeanServerFactory.create(cfg);
23+
24+
}
25+
26+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.ebean.model;
2+
3+
import javax.persistence.Entity;
4+
5+
@Entity
6+
public class Address extends BaseModel {
7+
8+
public Address(String addressLine1, String addressLine2, String city) {
9+
super();
10+
this.addressLine1 = addressLine1;
11+
this.addressLine2 = addressLine2;
12+
this.city = city;
13+
}
14+
15+
private String addressLine1;
16+
private String addressLine2;
17+
private String city;
18+
19+
public String getAddressLine1() {
20+
return addressLine1;
21+
}
22+
23+
public void setAddressLine1(String addressLine1) {
24+
this.addressLine1 = addressLine1;
25+
}
26+
27+
public String getAddressLine2() {
28+
return addressLine2;
29+
}
30+
31+
public void setAddressLine2(String addressLine2) {
32+
this.addressLine2 = addressLine2;
33+
}
34+
35+
public String getCity() {
36+
return city;
37+
}
38+
39+
public void setCity(String city) {
40+
this.city = city;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "Address [id=" + id + ", addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + "]";
46+
}
47+
48+
}

0 commit comments

Comments
 (0)