Issue
I have maintained OneToOne unidirectional join but now I need bi-directional join. I dont want to make changes in my code.
I have searched on google for alternate solution but found nothing.
@OneToOne(fetch=FetchType.EAGER , cascade=CascadeType.ALL)
@JoinColumn(name="ProductID",referencedColumnName="ProductID")
private AddProduct addProd;
I am able to get object of AddProduct but now I want to get object of associated class using AddProduct or variable/columns of AddProduct table but dont want to make changes in this class (AddProduct)
Solution
There is one solution if you don't want to change your code then you can do one thing you have to make SQL join query using
session.createSQLQuery("db_query");
get all the records and set them to both the model classes. After that, you can use both the model object as per requirement. For eg you can get addProd directly using
@OneToOne
private AddProduct addProd;
but in other case, you will not have bidirectional join, so you have to do it manually.
in DAO // Grocery is dummy model class fro explanation
List<Grocery> g = new ArrayList();
List<AddProduct> ap = new ArrayList();
List common = new ArrayList();
Grocery grocery = new Grocery();
grocery.set(".....");
..........
g.add(grocery);
AddProduct product = new product();
product.set(".....");
...............
ap.add(product);
common.add(g);
common.add(ap);
return common;
in Controller
model.addAttribute("grocery",common.get(0));
model.addAttribute("productcommons",common.get(1));
You will have same thing as per OneToOne join. In this way you can use bidirectional join without hampering the code.
Answered By - Satish Verma