Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7993573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:53:31+00:00 2026-06-04T13:53:31+00:00

I am exploring JPA and Ebean within Play Framework 2.0.1 to research what is

  • 0

I am exploring JPA and Ebean within Play Framework 2.0.1 to research what is generated in a local relational database. In my tests, I’m using MariaDB, a fork of MySQL.

So far, I’ve been able to successfully generate tables in the database using the JPA annotations within my models. I wanted to make sure that I learned how to set up an entity with a OneToOne relationship to another entitiy, as well as a OneToMany to another.

OneToOne: Employee has Address

OneToMany: Employee has Pet(s)

I set up a small application that allows me to quickly populate the tables, display some of the info, and delete rows from the tables. Here is the code below for those who want to see my setup:

Routes

GET     /                   controllers.Application.index()
GET     /add                controllers.Application.addEmployee()
GET     /delete/:id         controllers.Application.deleteEmployee(id: Long)
GET     /employee/:id       controllers.Application.showEmployee(id: Long)

Controller

Application.java

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("entry point"));
    }

    public static Result addEmployee() {
        // Create an employee.
        Employee employee = new Employee();     
        employee.firstName = "John";
        employee.lastName = "Doe";
        employee.salary = new BigDecimal(123456);

        // Create an address for the employee.
        Address address = new Address();
        address.city = "West Chester";
        address.country = "United States of America";
        address.postalCode = "19380";
        address.province = null;
        address.street = "45 Jingleheimer Drive";

        // Create pets for the employee.
        Pet pet = new Pet();
        pet.petString = "dog";
        Pet pet2 = new Pet();
        pet2.petString = "cat";

        employee.address = address;
        employee.pets.add(pet);
        employee.pets.add(pet2);
        employee.save();

        return ok(index.render("added an employee"));
    }

    public static Result showEmployee(Long id) {
        Employee e = Employee.get(id);
        //String s = e.address.street;
        return ok(employee.render(e));
    }

    public static Result deleteEmployee(Long id) {
        Employee.delete(id);
        return ok(index.render("deleted employee " + id));
    }
}

Models

Employee.java

@Entity
public class Employee extends Model {
    @Id
    public Long employee_id;

    public String firstName;

    public String lastName;

    public BigDecimal salary;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="owner_id")
    public Address address;

    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="owner_id", referencedColumnName="employee_id")
    public List<Pet> pets;

    public static Employee get(Long id) {
        return find.byId(id);
    }

    public static void delete(Long id) {
        find.byId(id).delete();
    }

    public static Finder<Long, Employee> find = new Finder<Long,Employee>(Long.class, Employee.class);
}

Address.java

@Entity
public class Address extends Model {
    @Id
    public Long address_id;

    public String street;

    public String city;

    public String province;

    public String country;

    public String postalCode;
}

Pet.java

@Entity
public class Pet extends Model {
    @Id
    public Long pet_id;
    public String petString;    
}

Views

employee.scala.html

@(employee: models.Employee)

@import helper._

@main("employee view") {

    <p>@employee.firstName</p>
    <p>@employee.lastName</p>
    <p>@employee.salary</p>
    <p>@employee.address.country</p>

    <p>@employee.pets(1).petString</p>

    @for(pet <- employee.pets) {
        <p>@pet.petString</p>
    }
}

If I render the view above using a URL like localhost:9000/employee/1 I get the following displayed in the browser:

John

Doe

123456

cat

dog

cat

Notice that the address’s country is not displayed. I’ve also tried accessing it in the controller and printing it to the command window, which prints NULL.

However, I found that if I added getters to the Address class, I can retrieve it in the scala template:

Adding

public String getCity() {
    return city;
}

...

to Address.java results in

John

Doe

123456

United States of America

cat

dog

cat

So I am able to set an address and assign it to an employee (it shows up in my DB). Also, deleting an employee correctly cascades and deletes the Address in the database. Likewise, this works for the Pet class which is OneToMany. So why do I need to add getters to access my Address properties, but not for Pet? Is my one-to-one relationship not set up correctly in the Java annotations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:53:33+00:00Added an answer on June 4, 2026 at 1:53 pm

    EBean does not support lazy loading with direct property access.
    Within Java-Controller-Code, the statement employee.address.country is enhanced to something like employee.getAddress().getCountry(). This, alas, is not done in Scala code, specifically not in the templates.

    employee.pets(1).petString, on the other hand, is translated to employee.pets.get(1).petString, which is enough for EBean to lazy load, because an EBean LazyList is involved.

    Don’t stick back to getters/setters please! The Play! people argue you should know in the controller what data you need and fetch it at query time:

    public static Employee get(Long id) {
      return find.fetch("address").where().idEq(id).findUnique();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been exploring different strategies for running integration tests within some Nant build scripts.
I was exploring possibilities of Rich Internet applications using Python. The most awesome possibility
I'm exploring the idea of using contenteditable on a website at visitor comments and
Exploring around S3's UI, it seems they only enjoy file uploads from my local
Recently I have been exploring using the fancy new dynamic keyword introduced in C#
I am exploring the idea of using professionally designed vector images as the ControlTemplates
I am using spring,JPA,jsf for my application and webserver is tomcat.In my application i
I am exploring WatIN automation framework driven by NUnit and Nant. I have Apartmentstate
While exploring msdn sites ,most of the condition checking places they are using (NULL
I have a web app using JSF2 with JPA Entities, Stateless ejb session beans

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.