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 9066249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:41:40+00:00 2026-06-16T16:41:40+00:00

I am writing an application using the Play framework, which uses JPA annotations and

  • 0

I am writing an application using the Play framework, which uses JPA annotations and Hibernate as part of the included Ebean ORM persistence layer. I’m having some trouble figuring out the JPA annotations related to foreign keys. In my MySQL database, there is one main table called Locations that stores addresses (street, city, state, zip, etc). Many many other tables in the database use this table in either one-to-one or many-to-many relationships. The problem is, I get an error when I try to map multiple fields to the id field in Locations.

For example, Docks and LineItems are two models that contain references to a Location. Each Dock has a Location, and each LineItem has two Locations: an origin and a destination.

So, in Dock, I have the code:

@OneToOne(optional=false)
@JoinColumn(name="loc_id")
public Location location;

and in LineItem, I have:

@OneToOne(optional=false)
@JoinColumn(name="origin")
public Location origin;

@OneToOne(optional=false)
@JoinColumn(name="destination")
public Location destination;

but I can’t map them all to the id field in Locations because JPA won’t allow me to do this:

@Id
@GeneratedValue
@OneToOne(mappedBy="location", fetch=FetchType.EAGER)
@OneToOne(mappedBy="origin", fetch=FetchType.EAGER)
@OneToOne(mappedBy="destination", fetch=FetchType.EAGER)
@Column(name="loc_id")
public Long id;

I have a feeling that I’m just going about this the wrong way. Is there a simpler way to have a foreign key constraint in JPA? I don’t really want to use a work-around like creating a bunch of extra columns in my tables because Locations really is referenced a LOT. Any advice to set me straight would be appreciated.

  • 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-16T16:41:40+00:00Added an answer on June 16, 2026 at 4:41 pm

    The Location config you’ve shown doesn’t make any sense. Here’s what it would mean to JPA if it could be interpreted with your full intent:

    1. Location has a Long “id” property which is a generated value and is mapped to the column loc_id.
    2. The “id” property is a Dock and is the non-owning end of a one-to-one, bidirectional relationship between Dock and Location.
    3. It’s also a LineItem that’s the non-owning end of a one-to-one, bidirectional relationship from LineItem as both the origin and the destination.

    Obviously that doesn’t make any sense, right? If you were going to have all these @OneToOne annotations in Location, each one would need to be on its own field: one Dock and two LineItems, plus more for all of the other relationships to Location that you mentioned. That’s obviously not desirable because a Location is just that, and it doesn’t matter what other things reference it. So what you really want is a bunch of unidirectional relationships to Location. Since you’ve already got Location mapped as the non-owning end (via the “mappedBy” parameter), all you have to do is remove the @OneToOnes from Location. All you need is, for example:

    @OneToOne
    @JoinColumn(name="origin")
    public Location origin;
    

    That says that the “origin” property is the Location whose primary key is stored in the “origin” field of the table this entity is mapped to.

    On that note, I expect all of these aren’t really one-to-one relationships. At least the LineItem ones are one-to-many. I can’t imagine that two line items would never have the same origin or destination.

    And by the way, optional=false is the default, but if you’re thinking it prevents that field from being null, it doesn’t. The optional parameter just tells JPA whether or not to throw an exception if the row referenced by the foreign key is missing.

    That’s kind of a high-level overview. Ask further questions if you don’t understand some of it. The Hibernate Annotations guide should help you understand a lot of it, and it can also be very helpful to have hibernate generate a database schema for you so you can see if it’s understanding what you’re telling it as far as table structure.

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

Sidebar

Related Questions

I've recently started using the Play! framework (v2.0.4) for writing a Java web application.
Writing a Java application using the Play framework and need some HTTP-Live streaming. I
I am writing a web-application using Play 1.2.3 . One of the feature is
I'm a writing an application using Spring MVC which has to run as a
I am thinking of writing a web app in Scala using the Play! framework
I'm writing a desktop application in Python using the Qt framework, and it involves
I'm working on a web application using Java Play Framework 2.0 that heavily relies
I'm writing an application using the Spring Framework. Description of Exercise: Write a program
I am writing an application using cocoa which, in some point (surprise), opens window.
I'm currently writing some unit tests for a play application in scala using the

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.