my database tables:
cities(id serial, name varchar(40);
weather(id serial, city_id int, temp int, date date)
cities.id = weather.city_id
In Spring I have same POJO as the fields in database.
for example City.java:
@Entity
@Table(name="CITIES")
public class City {
@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "CITIES_ID_SEQ")
@SequenceGenerator(name = "CITIES_ID_SEQ", sequenceName="cities_id_seq", allocationSize=1)
private Integer id;
@Column(name="NAME")
private String name;
//here come getters and setters
DAO – so this will be returned to controller, which sends it to JSP:
public Weather getWeatherById(Integer id) {
return (Weather) sessionFactory.getCurrentSession().get(Weather.class, id);
}
Controller:
model.addAttribute("weather", weatherService.getWeatherById(id));
Question is that, how can I access the cities.name from JSP? Or that is not possible, without special query?
These two objects should have biderectional one-to-one releationship.
and the second class:
then you can fetch city and get its weather using getWeather() or you can fetch weather and get its city.
Alternatively, you can use HQL join statement to fetch city: