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
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:
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.