开发者

Accessing object properties with JSTL - Spring and hibernate

I have Persons(id, name,..), Division(id, name, parentId, level...), and PersonDivisoin (id, personId, divisionId....) tables. Application uses hibernate framework to map relations with onetoone mapping between Person and Person Division, onetomany mappings between Division and PersonDivision.. below is an example data from Division table..

 _ _ _ _ _ _ __ _ _ _ _ _ _ _ 
| id | name | parentId| level|
 -----------------------------
   1   XYZ               dept
   2   ZYW      1        team

and PersonDivision table is...

_ _ _ _ _ _ _ _ _ _ _ _ __ _ _
| id | personid | divisionId| 
------------------------------
  1      1           2

So I am having trouble getting the value of parent division name on the front end side if I have a person object..

Person class looks something like this..

 class Person{
   String name;
   int id;
   PersonDivision personDivision;
 }

PersonDivision class..

 class PersonDivision
 {
    int id;
    @OneToOne Person person;
    @ManytoOne Division division;
}

on the front page(jsp), if have to access a Person's team, I can do something like, ${person.personDivisoin.divsion.name}

but how would I access Person's "department"? Ideally it should take the parentid from "Division" table and get the name from it.. ${person.personDivisoin.divsion.parentId} gives Parent Division id but I am not sure how to retrieve its name with using JSTL.开发者_如何学JAVA. the best I could think of is that, sending a list of all the divisions as a 2-D array with id and name and on the view, retreieve name from id..

I would like to know if there are any better/simpler methods to do this.. thanks in advance!


Why do you need a PersonDivision class? Since the association between PersonDivision and Person is mapped as @OneToOne wouldn't it be easier to just map the Division as a field in Person?

Something like this

class Person {
   @Column
   String name;

   @Id
   int id;

   @ManyToOne
   Division division;
}

This would allow you to access the division name as ${person.division.name}.

In addition to that, if you define the Division as follows, you should be able to access the parent division with the same ease (${person.division.parent.name} to get the parent name).

class Division {
   @Column
   String name;

   @Id
   int id;

   @ManyToOne
   Division parent;
}

You can find more info and examples on Hibernate documentation about associations


Instead of parentId, you can have @ManyToOne Division parentDivision. It will be the same in the database, but hibernate will fetch the parent division (and the whole hierarchy, actually, if you don't set the relation to lazy).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜