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

  • SEARCH
  • Home
  • 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 4604134
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:15:06+00:00 2026-05-22T00:15:06+00:00

Suppose I have 2 models Public class Bus extends Model{ @OneToMany(mappedBy=bus) private List<Passenger> passengers;

  • 0

Suppose I have 2 models

Public class Bus extends Model{

    @OneToMany(mappedBy="bus")
    private List<Passenger> passengers;

    public Bus(passengers){
        this.passengers=passengers;
    }

    public List<Passenger> getPassengers(){
        return passengers;
    }
}

public Class Passenger extends Model{

    @ManyToOne
    private Bus bus;

    public Passenger(Bus bus){
        this.bus=bus;
    }
}

Can I use a controller method that finds all the passengers in a bus using the getter. eg:

public static void getPassengers(Long busId){
    Bus bus = Bus.findById(busId);
    List<Passengers> pList = bus.getPassengers();
}

I tried it in a real play web application but the List size returned is always 0.

Thanks in advance.

  • 1 1 Answer
  • 2 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-05-22T00:15:07+00:00Added an answer on May 22, 2026 at 12:15 am

    Can I use a controller method that finds all the passengers in a bus using the getter?

    Yes, you can. But I think the problem isn’t using a getter, it is that you have not constructed your models correctly.

    There are 3 problems I see in your example:

    1. In your controller action getPassengers(Long busId) you need to change List<Passengers> to List<Passenger> (singular “Passenger”)

    2. In your Bus model class, you need to either make passengers public or declare a public setter. The “Play!” way is to just declare fields as public and let the framework generate the getters and setters for you. The only reason you would declare your own getters and setters is if you needed to add some logic.

    3. In your Passenger model class, you need to make bus public.

    Here’s how I would fix your code:

    public class Bus extends Model{
    
        @OneToMany(mappedBy="bus")
        public List<Passenger> passengers;
    
        public Bus(passengers){
            this.passengers=passengers;
        }
    
        // No need for getters or setters, but we'll add a getter for the sake of this question:
        public List<Passenger> getPassengers() {
            return this.passengers;
        }
    }
    
    public class Passenger extends Model{
    
        @ManyToOne
        public Bus bus;
    
        public Passenger(Bus bus){
            this.bus = bus;
        }
    }
    

    And your controller action:

    public static void getPassengers(Long busId){
        Bus bus = Bus.findById(busId);
        List<Passenger> pList = bus.getPassengers();
        render(pList); // or whatever rendering you want to do
    }
    

    If this doesn’t fix your problem, then you are not loading your data correctly. You should post the code you are running to actually create the records – whether that be loading a YAML file or whatever.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this model: public class ViewModel { [Required] public string UserInput {
Suppose, I have models: public class Person { public sting Name {get;set;} public List<Book>
Suppose I have the view models below. public class CustomerListViewModel { public IEnumerable<CustomerViewModel> Customers
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose I have my models set up already. class books(models.Model): title = models.CharField... ISBN
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose the following model public class Product { private Integer requiredQuantity; private Integer allowedQuantity;
Suppose I have 2 models. The 2nd model has a one-to-one relationship with the
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a

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.