【发布时间】:2011-09-23 20:16:36
【问题描述】:
我的数据库表:
cities(id serial, name varchar(40);
weather(id serial, city_id int, temp int, date date)
cities.id = weather.city_id
在 Spring 中,我拥有与数据库中的字段相同的 POJO。
例如 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 - 所以这将返回给控制器,控制器将其发送到 JSP:
public Weather getWeatherById(Integer id) {
return (Weather) sessionFactory.getCurrentSession().get(Weather.class, id);
}
控制器:
model.addAttribute("weather", weatherService.getWeatherById(id));
问题是,如何从 JSP 访问cities.name?或者,如果没有特殊查询,这是不可能的?
【问题讨论】:
-
城市是天气抽象的一部分吗?如果是,您可以从那里访问它和它的名称。
-
@mkro : 你在抽象下是什么意思?
-
正如@danny.lesnik 的回答所暗示的那样:您需要一个天气信息类,并且该类应该连接到城市。
标签: hibernate spring jsp spring-mvc