|
| 1 | +== Ebean |
| 2 | + |
| 3 | +Persistence module using Ebean: https://ebean.io |
| 4 | + |
| 5 | +=== Usage |
| 6 | + |
| 7 | +1) Add the dependencies (hikari + ebean): |
| 8 | + |
| 9 | +[dependency, artifactId="jooby-hikari:DataSource via HikariCP, jooby-ebean:Ebean Module"] |
| 10 | +. |
| 11 | + |
| 12 | +2) Add database driver (mySQL here): |
| 13 | + |
| 14 | +[dependency, artifactId="mysql-connector-java"] |
| 15 | +. |
| 16 | + |
| 17 | +3) Set database properties |
| 18 | + |
| 19 | +.application.conf |
| 20 | +[source, properties] |
| 21 | +---- |
| 22 | +db.url = "jdbc:mysql://localhost/mydb" |
| 23 | +db.user = myuser |
| 24 | +db.password = mypass |
| 25 | +---- |
| 26 | + |
| 27 | +4) Configure build time enhancement of the entity beans |
| 28 | + |
| 29 | +.Maven |
| 30 | +[source, xml, role="primary", subs="verbatim,attributes"] |
| 31 | +---- |
| 32 | +<plugin> |
| 33 | + <groupId>io.repaint.maven</groupId> |
| 34 | + <artifactId>tiles-maven-plugin</artifactId> |
| 35 | + <version>${tiles-maven-plugin.version}</version> |
| 36 | + <extensions>true</extensions> |
| 37 | + <configuration> |
| 38 | + <tiles> |
| 39 | + <!-- other tiles ... --> |
| 40 | + <tile>io.ebean.tile:enhancement:{ebeanVersion}</tile> |
| 41 | + </tiles> |
| 42 | + </configuration> |
| 43 | +</plugin> |
| 44 | +---- |
| 45 | + |
| 46 | +.Gradle |
| 47 | +[source, javascript, role="secondary", subs="verbatim,attributes"] |
| 48 | +---- |
| 49 | +plugins { |
| 50 | + id('io.ebean') version '{ebeanVersion}' |
| 51 | +} |
| 52 | +
|
| 53 | +dependencies { |
| 54 | + compile 'io.ebean:ebean:{ebeanVersion}' |
| 55 | + compile 'io.ebean:ebean-querybean:{ebeanVersion}' |
| 56 | +
|
| 57 | + // query bean generation |
| 58 | + annotationProcessor 'io.ebean:querybean-generator:{ebeanVersion}' |
| 59 | +} |
| 60 | +---- |
| 61 | + |
| 62 | +4) Install and use Ebean |
| 63 | + |
| 64 | +.Java |
| 65 | +[source, java, role="primary"] |
| 66 | +---- |
| 67 | +import io.jooby.hikari.HikariModule; |
| 68 | +import io.jooby.ebean.EbeanModule; |
| 69 | +
|
| 70 | +{ |
| 71 | + install(new HikariModule()); <1> |
| 72 | +
|
| 73 | + install(new EbeanModule()); <2> |
| 74 | +
|
| 75 | + get("/", ctx -> { |
| 76 | + Database db = require(Database.class); <3> |
| 77 | + // work with Database |
| 78 | + }); |
| 79 | +} |
| 80 | +---- |
| 81 | + |
| 82 | +.Kotlin |
| 83 | +[source, kt, role="secondary"] |
| 84 | +---- |
| 85 | +import io.jooby.hikari.HikariModule |
| 86 | +import io.jooby.ebean.EbeanModule |
| 87 | +
|
| 88 | +{ |
| 89 | + install(HikariModule()) <1> |
| 90 | + |
| 91 | + install(EbeanModule()) <2> |
| 92 | +
|
| 93 | + get("/") { |
| 94 | + val db = require(Database::class) <3> |
| 95 | + // work with Database |
| 96 | + } |
| 97 | +} |
| 98 | +---- |
| 99 | + |
| 100 | +<1> Install and creates a `DataSource` |
| 101 | +<2> Install and Ebean |
| 102 | +<3> Use Ebean Database |
| 103 | + |
| 104 | +=== Transactional Request |
| 105 | + |
| 106 | +The javadoc:ebean.TransactionalRequest[] decorator takes care of a start/commit/rollback a |
| 107 | +transaction per HTTP request. |
| 108 | + |
| 109 | +.TransactionalRequest |
| 110 | +[source, java, role = "primary"] |
| 111 | +---- |
| 112 | +import io.jooby.hikari.HikariModule; |
| 113 | +import io.jooby.ebean.EbeanModule; |
| 114 | +import io.jooby.ebean.TransactionalRequest; |
| 115 | +
|
| 116 | +{ |
| 117 | + install(new HikariModule()); |
| 118 | + |
| 119 | + install(new HibernateModule()); |
| 120 | + |
| 121 | + decorator(new TransactionalRequest()); |
| 122 | + |
| 123 | + post("/create", ctx -> { |
| 124 | + Database db = require(Database.class); |
| 125 | + |
| 126 | + MyEntity e = ...; |
| 127 | + |
| 128 | + db.save(e); |
| 129 | +
|
| 130 | + return e; |
| 131 | + }); |
| 132 | +} |
| 133 | +---- |
| 134 | + |
| 135 | +.Kotlin |
| 136 | +[source, kt, role="secondary"] |
| 137 | +---- |
| 138 | +import io.jooby.hikari.HikariModule |
| 139 | +import io.jooby.ebean.EbeanModule |
| 140 | +import io.jooby.ebean.TransactionalRequest |
| 141 | +
|
| 142 | +{ |
| 143 | + install(HikariModule()) |
| 144 | + |
| 145 | + install(HibernateModule()) |
| 146 | + |
| 147 | + decorator(TransactionalRequest()) |
| 148 | + |
| 149 | + post("/create") { ctx -> |
| 150 | + val db = require(Database::class) |
| 151 | + |
| 152 | + val e = ... |
| 153 | + |
| 154 | + db.save(e) |
| 155 | +
|
| 156 | + e |
| 157 | + } |
| 158 | +} |
| 159 | +---- |
| 160 | + |
| 161 | +=== Configuration |
| 162 | + |
| 163 | +Advanced/Custom configuration is supported programmatically or using property files. |
| 164 | + |
| 165 | +.Programmatically |
| 166 | +[source, java, role="primary"] |
| 167 | +---- |
| 168 | +{ |
| 169 | + DatabaseConfig dbConfig = ...; <1> |
| 170 | + install(new EbeanModule(dbConfig)); <2> |
| 171 | +} |
| 172 | +---- |
| 173 | + |
| 174 | +.Kotlin |
| 175 | +[source, kt, role="secondary"] |
| 176 | +---- |
| 177 | +{ |
| 178 | + val dbConfig = ... <1> |
| 179 | + install(EbeanModule(dbConfig)) <2> |
| 180 | +} |
| 181 | +---- |
| 182 | + |
| 183 | +<1> Manually creates a database config or use the one provided by Jooby: javadoc:ebean.EbeanModule[create, io.jooby.Jooby, java.lang.String]. |
| 184 | +<2> Install Ebean with custom database config |
| 185 | + |
| 186 | +.Configuration |
| 187 | +[source,javascript] |
| 188 | +---- |
| 189 | +{ |
| 190 | + ebean { |
| 191 | + ddl { |
| 192 | + generate = true |
| 193 | + run = true |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | +---- |
| 198 | + |
| 199 | +Example shows how to setup Ebean migration tools. Keep in mind Jooby offers a better solution for |
| 200 | +database migrations link:flyway[Flyway Module]. |
0 commit comments