Merge pull request #5336 from josephine-barboza/master
BAEL-2169 Guide to Ebean
This commit is contained in:
commit
90867eb6b4
|
@ -148,42 +148,42 @@
|
||||||
<version>${jmapper.version}</version>
|
<version>${jmapper.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- crunch project -->
|
<!-- crunch project -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.crunch</groupId>
|
<groupId>org.apache.crunch</groupId>
|
||||||
<artifactId>crunch-core</artifactId>
|
<artifactId>crunch-core</artifactId>
|
||||||
<version>${org.apache.crunch.crunch-core.version}</version>
|
<version>${org.apache.crunch.crunch-core.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.hadoop</groupId>
|
<groupId>org.apache.hadoop</groupId>
|
||||||
<artifactId>hadoop-client</artifactId>
|
<artifactId>hadoop-client</artifactId>
|
||||||
<version>${org.apache.hadoop.hadoop-client}</version>
|
<version>${org.apache.hadoop.hadoop-client}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-cli</groupId>
|
<groupId>commons-cli</groupId>
|
||||||
<artifactId>commons-cli</artifactId>
|
<artifactId>commons-cli</artifactId>
|
||||||
<version>1.2</version>
|
<version>1.2</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>2.1</version>
|
<version>2.1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-httpclient</groupId>
|
<groupId>commons-httpclient</groupId>
|
||||||
<artifactId>commons-httpclient</artifactId>
|
<artifactId>commons-httpclient</artifactId>
|
||||||
<version>3.0.1</version>
|
<version>3.0.1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>commons-codec</groupId>
|
<groupId>commons-codec</groupId>
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.flink</groupId>
|
<groupId>org.apache.flink</groupId>
|
||||||
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
|
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
|
||||||
|
@ -249,7 +249,32 @@
|
||||||
<version>${awaitility.version}</version>
|
<version>${awaitility.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<artifactId>ebean</artifactId>
|
||||||
|
<version>${ebean.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-core</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>${slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -354,31 +379,48 @@
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
<version>2.3</version>
|
<version>2.3</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<descriptors>
|
<descriptors>
|
||||||
<descriptor>src/main/assembly/hadoop-job.xml</descriptor>
|
<descriptor>src/main/assembly/hadoop-job.xml</descriptor>
|
||||||
</descriptors>
|
</descriptors>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<mainClass>com.baeldung.crunch.WordCount</mainClass>
|
<mainClass>com.baeldung.crunch.WordCount</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
</configuration>
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>make-assembly</id>
|
<id>make-assembly</id>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>single</goal>
|
<goal>single</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>io.ebean</groupId>
|
||||||
|
<artifactId>ebean-maven-plugin</artifactId>
|
||||||
|
<version>11.11.2</version>
|
||||||
|
<executions>
|
||||||
|
<!-- enhance main classes -->
|
||||||
|
<execution>
|
||||||
|
<id>main</id>
|
||||||
|
<phase>process-classes</phase>
|
||||||
|
<configuration>
|
||||||
|
<transformArgs>debug=1</transformArgs>
|
||||||
|
</configuration>
|
||||||
|
<goals>
|
||||||
|
<goal>enhance</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
@ -416,6 +458,9 @@
|
||||||
<jmapper.version>1.6.0.1</jmapper.version>
|
<jmapper.version>1.6.0.1</jmapper.version>
|
||||||
<org.apache.crunch.crunch-core.version>0.15.0</org.apache.crunch.crunch-core.version>
|
<org.apache.crunch.crunch-core.version>0.15.0</org.apache.crunch.crunch-core.version>
|
||||||
<org.apache.hadoop.hadoop-client>2.2.0</org.apache.hadoop.hadoop-client>
|
<org.apache.hadoop.hadoop-client>2.2.0</org.apache.hadoop.hadoop-client>
|
||||||
|
<ebean.version>11.22.4</ebean.version>
|
||||||
|
<slf4j.version>1.7.25</slf4j.version>
|
||||||
|
<logback.version>1.0.1</logback.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
package com.baeldung.ebean.app;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import com.baeldung.ebean.model.Address;
|
||||||
|
import com.baeldung.ebean.model.Customer;
|
||||||
|
|
||||||
|
import io.ebean.Ebean;
|
||||||
|
import io.ebean.EbeanServer;
|
||||||
|
import io.ebean.annotation.Transactional;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
insertAndDeleteInsideTransaction();
|
||||||
|
crudOperations();
|
||||||
|
queryCustomers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public static void insertAndDeleteInsideTransaction() {
|
||||||
|
|
||||||
|
Customer c1 = getCustomer();
|
||||||
|
EbeanServer server = Ebean.getDefaultServer();
|
||||||
|
server.save(c1);
|
||||||
|
Customer foundC1 = server.find(Customer.class, c1.getId());
|
||||||
|
server.delete(foundC1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void crudOperations() {
|
||||||
|
|
||||||
|
Address a1 = new Address("5, Wide Street", null, "New York");
|
||||||
|
Customer c1 = new Customer("John Wide", a1);
|
||||||
|
|
||||||
|
EbeanServer server = Ebean.getDefaultServer();
|
||||||
|
server.save(c1);
|
||||||
|
|
||||||
|
c1.setName("Jane Wide");
|
||||||
|
c1.setAddress(null);
|
||||||
|
server.save(c1);
|
||||||
|
|
||||||
|
Customer foundC1 = Ebean.find(Customer.class, c1.getId());
|
||||||
|
|
||||||
|
Ebean.delete(foundC1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void queryCustomers() {
|
||||||
|
Address a1 = new Address("1, Big Street", null, "New York");
|
||||||
|
Customer c1 = new Customer("Big John", a1);
|
||||||
|
|
||||||
|
Address a2 = new Address("2, Big Street", null, "New York");
|
||||||
|
Customer c2 = new Customer("Big John", a2);
|
||||||
|
|
||||||
|
Address a3 = new Address("3, Big Street", null, "San Jose");
|
||||||
|
Customer c3 = new Customer("Big Bob", a3);
|
||||||
|
|
||||||
|
Ebean.saveAll(Arrays.asList(c1, c2, c3));
|
||||||
|
|
||||||
|
Customer customer = Ebean.find(Customer.class)
|
||||||
|
.select("name")
|
||||||
|
.fetch("address", "city")
|
||||||
|
.where()
|
||||||
|
.eq("city", "San Jose")
|
||||||
|
.findOne();
|
||||||
|
|
||||||
|
Ebean.deleteAll(Arrays.asList(c1, c2, c3));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Customer getCustomer() {
|
||||||
|
Address a1 = new Address("1, Big Street", null, "New York");
|
||||||
|
Customer c1 = new Customer("Big John", a1);
|
||||||
|
return c1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.ebean.app;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import io.ebean.EbeanServer;
|
||||||
|
import io.ebean.EbeanServerFactory;
|
||||||
|
import io.ebean.config.ServerConfig;
|
||||||
|
|
||||||
|
public class App2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ServerConfig cfg = new ServerConfig();
|
||||||
|
cfg.setDefaultServer(true);
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.put("ebean.db.ddl.generate", "true");
|
||||||
|
properties.put("ebean.db.ddl.run", "true");
|
||||||
|
properties.put("datasource.db.username", "sa");
|
||||||
|
properties.put("datasource.db.password", "");
|
||||||
|
properties.put("datasource.db.databaseUrl", "jdbc:h2:mem:app2");
|
||||||
|
properties.put("datasource.db.databaseDriver", "org.h2.Driver");
|
||||||
|
cfg.loadFromProperties(properties);
|
||||||
|
EbeanServer server = EbeanServerFactory.create(cfg);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.ebean.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Address extends BaseModel {
|
||||||
|
|
||||||
|
public Address(String addressLine1, String addressLine2, String city) {
|
||||||
|
super();
|
||||||
|
this.addressLine1 = addressLine1;
|
||||||
|
this.addressLine2 = addressLine2;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String addressLine1;
|
||||||
|
private String addressLine2;
|
||||||
|
private String city;
|
||||||
|
|
||||||
|
public String getAddressLine1() {
|
||||||
|
return addressLine1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddressLine1(String addressLine1) {
|
||||||
|
this.addressLine1 = addressLine1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddressLine2() {
|
||||||
|
return addressLine2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddressLine2(String addressLine2) {
|
||||||
|
this.addressLine2 = addressLine2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCity() {
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCity(String city) {
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Address [id=" + id + ", addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package com.baeldung.ebean.model;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.MappedSuperclass;
|
||||||
|
import javax.persistence.Version;
|
||||||
|
|
||||||
|
import io.ebean.annotation.WhenCreated;
|
||||||
|
import io.ebean.annotation.WhenModified;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
public abstract class BaseModel {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
protected long id;
|
||||||
|
|
||||||
|
@Version
|
||||||
|
protected long version;
|
||||||
|
|
||||||
|
@WhenCreated
|
||||||
|
protected Instant createdOn;
|
||||||
|
|
||||||
|
@WhenModified
|
||||||
|
protected Instant modifiedOn;
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Instant getCreatedOn() {
|
||||||
|
return createdOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedOn(Instant createdOn) {
|
||||||
|
this.createdOn = createdOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Instant getModifiedOn() {
|
||||||
|
return modifiedOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModifiedOn(Instant modifiedOn) {
|
||||||
|
this.modifiedOn = modifiedOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(long version) {
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.ebean.model;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Customer extends BaseModel {
|
||||||
|
|
||||||
|
public Customer(String name, Address address) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@OneToOne(cascade = CascadeType.ALL)
|
||||||
|
Address address;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
entity-packages: com.baeldung.ebean.model
|
||||||
|
transactional-packages: com.baeldung.ebean.app
|
||||||
|
querybean-packages: com.baeldung.ebean.app
|
|
@ -0,0 +1,7 @@
|
||||||
|
ebean.db.ddl.generate=true
|
||||||
|
ebean.db.ddl.run=true
|
||||||
|
|
||||||
|
datasource.db.username=sa
|
||||||
|
datasource.db.password=
|
||||||
|
datasource.db.databaseUrl=jdbc:h2:mem:customer
|
||||||
|
datasource.db.databaseDriver=org.h2.Driver
|
|
@ -6,7 +6,9 @@
|
||||||
</pattern>
|
</pattern>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
|
<logger name="io.ebean.DDL" level="TRACE"/>
|
||||||
|
<logger name="io.ebean.SQL" level="TRACE"/>
|
||||||
|
<logger name="io.ebean.TXN" level="TRACE"/>
|
||||||
<root level="INFO">
|
<root level="INFO">
|
||||||
<appender-ref ref="STDOUT" />
|
<appender-ref ref="STDOUT" />
|
||||||
</root>
|
</root>
|
||||||
|
|
Loading…
Reference in New Issue