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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:53:45+00:00 2026-05-25T21:53:45+00:00

What is the best/common solution (best practices) to working with relationships in ORM (from

  • 0

What is the best/common solution (best practices) to working with relationships in ORM (from long-life project view)?

1) E.g. I have Oracle HR schema. Is it ok to create relations between every objects (entities) where it is possible or only between objects where I really need now (and change in future)?

@Entity
public class Employee {
    @Id
    private long employeeId;
    @ManyToOne((fetch = FetchType.LAZY)
    private Job job;
}

@Entity
public class Job {
    @Id
    private String jobId;
    @OneToMany(fetch = FetchType.LAZY)
    private List<Employee> employeeList;
}

2) How should I store/set new Employee with existing or new Job? We have Employee entity with String jobId and also created relation Job job variable:

@Entity
public class Employee {
    @Id
    private long employeeId;
    private String jobId;
    @ManyToOne((fetch = FetchType.LAZY)
    private Job job;
}

The worst solutions is set both parameters everytime:

Job job = new Job();
job.setJobId("JOB_01");
Employee employee = new Employee();
employee.setEmployeeId(1L)
employee.setJobId(job.getJobId());

A little bit better solutions is re-implement setJobId() method in Employee class (there should be also some code to find out if mentioned Job exist or not):

public void setJobId(String jobId) {
    if (getJob() == null) {
        this.jobId = jobId;
        job = new Job();
        job.setJobId(jobId);
    } else {
        this.jobId = getJob().getJobId();
    }
}
  • 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-05-25T21:53:46+00:00Added an answer on May 25, 2026 at 9:53 pm

    If you don’t need an association, then you don’t need to add it. Note however that even if it’s not used in Java code directly, an association might still be useful just to be able to traverse it in JPQL or criteria queries.

    Regarding your jobId field, it should just be removed from the Employee entity. An employee has a job, and you can access its job ID using employee.getJob().getId(). You shouldn’t be able to set the job ID of an employee. Instead, you should get or create a job (in the service layer), and set the employee’s job using setJob(job).

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

Sidebar

Related Questions

What's the most common/best way to setup a WCF service project and applications? Here's
Is there a best-practice or common way in JavaScript to have class members as
Are there any best practices or common solutions to adding support for static methods
Is there some common solution or best practice how to implement localized URL string
I'm trying to find some info on the best and most common RESTful url
What is the best way to share common objects between multiple views in an
What is the common way (or best practice) to optionally use JNI? E.g. I
What are the best ways (or at least most common ways) in ASP (VBScript)
I'm looking for a best way to implement common Windows keyboard shortcuts (for example
This is a common problem for all developers, I am looking for the best

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.