Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted March 21st, 2016
Ebean setup in new IDE environments, IntelliJ

IntelliJ lets you do the whole setup with no commands needed, hence making it easy and time saving. no extra configurations are needed. on other IDEs, all the necessary libraries are downloaded, dependency resolutions performed and project building is handled by the SBT build tool, which is an open source build tool for Java projects.performs same functions as the maven.

The following are steps necessary in creating our first successful table using Ebeans.

Step 1:

  • Start a new maven project
    intelliJ

  • Click next, enter project details (group id and artifact id)

  • Once you are done, click finish to generate the new maven project

Step 2:

Dependencies
  • On the pom.xml file, add a dependency to facilitate the use of Ebeans an the database
<dependencies>
    <dependency>
        <groupid>org.avaje.ebeanorm</groupid>
        <artifactid>avaje-ebeanorm</artifactid>
        <version>6.11.1</version>
    </dependency>

//dependency for database
    <dependency>
        <groupid>org.postgresql</groupid>
        <artifactid>postgresql</artifactid>
        <version>9.4-1200-jdbc41</version>
    </dependency>
</dependencies>

The second dependency appears as it is since I’m using PostgreSQL database.
If you are using a MySQL database then one would use

<dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
    <version>5.1.38</version>
</dependency>

Secondly, create a new package just to make the project look neat, and rename the package as desired.
In the package, create a new java class in the package. Here is a sample:

package entity;

        import javax.persistence.*;

@Entity
@Table(name = "users")
public class User {

    @Id

    @Column(name = "user_name", nullable = false)//column name
    String userName;

    @Column(name = "full_name", nullable = false)
    String fullName;

    @Column(length = 60, nullable = false, unique = true)
    String email;

    @Column(length = 50, name = "phone_number")
    String phoneNumber;

}

The above class defines the columns to be expected in the user table. A table with four columns, user_name, full_name, email and phone_number.

NOTE:

The class name is specified just before the beginning of the class by first notifying the system that you are creating an entity with a specific name, “users” e.g.

@Entity
@Table(name = "users")

Step 3:

With the entity done, we now need to load the database drivers and enable the table defined in the user.java class in (Step 2) to be generated

In this step we will crate a .properties file called “ebean.properties”

#load.properties.override=${CATALINA_HOME}/conf/myapp.ebean.properties

# generate DDL files
ebean.ddl.generate=true

# run ddl drops and recreates tables
ebean.ddl.run=true

ebean.uuidStoreAsBinary=true
ebean.databaseSequenceBatchSize=1

ebean.debug.sql=true
ebean.debug.lazyload=true

# json date format
ebean.jsonDateTime=ISO8601

# enable autofetch
ebean.autofetch.querytuning=true

# data source default
datasource.default=pg

# postgres
datasource.pg.username=postgres
datasource.pg.password=postgres
datasource.pg.schema=public
datasource.pg.url=jdbc:postgresql://localhost:port/database
datasource.pg.driver=org.postgresql.Driver
datasource.pg.minConnections=1
datasource.pg.maxConnections=50
datasource.pg.heartbeatsql=select 1

In this file, the following lines performs the table creation and dropping respectively

# generate DDL files
ebean.ddl.generate=true

# run ddl drops and recreates tables
ebean.ddl.run=true

It is however advisable that once the table(s) have been created, comment the lines you’re using the “#” sign. This is because whenever the project runs it will always drop all the tables and create new ones. Otherwise all data already saved in the table will be lost .

Step: 4

Finally, after the Ebean is completely set-up, the entity class well done and all necessary dependencies added in the pom.xml file, create a main class to test if your code runs well.
Use the

EbeanServerFactory.create("pg");

In your main class to establish the connection to the database and create the table(s).

The code creates the using ebean.properties to configure the server.

NOTE:

Running the project as it is will not be successful. The system will bring an error message saying “entity not enhanced”

Exception in thread "main" java.lang.IllegalStateException: Bean class entity.User is not enhanced?

the solution to this is to add the Ebean Orm Enhancer.
– go to file
– then settings
– select plugins
– search Ebean ORM enhancer
– install then restart the IDE

Once the IDE is ready, click on the uild tab and select the “Ebean ORM enhancer” then run the main class.

Posted by Bryant Smith

Leave a Reply

Your email address will not be published. Required fields are marked *