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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:17:21+00:00 2026-06-14T22:17:21+00:00

I am working with Java EE JSP/EJB3 and I have a problem implementing relations

  • 0

I am working with Java EE JSP/EJB3 and I have a problem implementing relations between tables. Initially I was thinking of storing the id but I’ve read that it would defeat the purpose of EJBs which store references to objects

As a test example to explain my problem I came up with a Car Model and a Car Brand relation. In this web application The user creates the brands first. then create the cars and select the brand from a dropdown.

Car ->rand: many to one
Brand->Car: one to many

public class Brand implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="id")
  private long id;

  @Column(name="brand_name")
  private String brand_name;

  @Column(name="cars_fk")
  private Set<Car> cars;
...

}

The following is the car Entitybean

public class Car implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="id")
  private long id;

  private Brand brand;

  @ManyToOne(fetch = FetchType.LAZY)
  @Column(name="brand_FK")
  private Brand brand;
...

}

Given that the above is correct (which I’m not sure of)

What is the best practice to handle inputting of these

lets say i have this html ( if the select has to be different please do tell )

<form action="processCar.jsp">
  Model: <input type="text" name="title"><br>

  <select>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </select> 

  <input type="submit" value="Submit">
</form> 

The brand options are loaded from the Brand.findAll.

Also if this is correct, where does the change over from brand name/id to an entity object Brand is done ? in the session bean ?

public void createCar(String title,String brandname){
   Query query = em.createNamedQuery("Brand.getBrandbyName");
   query.setParameter("brand_name", brandname);
   Brand brand = (Brand)query.getSingleResult();
   carSLSBManagement.create(title,brand);
}

Thanks in advance

  • 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-14T22:17:22+00:00Added an answer on June 14, 2026 at 10:17 pm

    The usual way to do this would be to use the ID as the value in the drop-down, and then to convert it to an object as soon as possible in the code that receives the request. That would be a servlet or equivalent, rather than a bean. The bean should work with objects that have already been fetched.

    If you have the ID of the brand, then fetching it is as simple as:

    long brandId = Long.parseLong(request.getParameter("brandId"));
    Brand brand = em.find(Brand.class, brandId);
    

    I will add that although my preference is to do all the parameter parsing and decoding in the web layer, before going to the domain layer, it might be more convenient for technical reasons for you to push some of it downwards into the domain layer. For example, you should have a transaction open when you do the em.find, and if you are using a classic Java EE architecture with servlets and EJBs, then transactions naturally begin at the EJBs, with the servlets being non-transactional. To fit in with that, you could parse the ID to a long in the servlet, but fetch the object in the EJB. That’s a little more clunky, but it does mean you don’t have to mess about with transactions.

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

Sidebar

Related Questions

Possible Duplicate: Relationship between JSP and Java EE I am working with JSP and
I'm working with Java and JSP. I have to create something like a promotion
I have a jsp code working fine on one of the machines. But when
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve
I'm fairly new to Android, but have done lots of Java/JSP development and HTML
New to java, but would love to implement my working jsp that generates xml
I'm working out of Murach's Java servlets and JSP, chapter 14 on connecting to
When working with Java from Scala, we have to account for null. HttpServletRequest getters
I'm working with java, trying to use the AudioFormat Class. I have the following
I have been working in Java/J2ee projects, in which I follow the Maven structure.

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.