我需要存储在我的类的数据库集合中,如下所示:
class City {
private String name;
private List<String> points;
}
所以我的表应该是这样的:
@Entity
class Cities {
private Date date;
private List<City> cities;
}
那么我如何在Spring Web应用程序中实现呢?
答案 0 :(得分:0)
Spring使用hibernate来处理数据库数据操作。
如果您将eclipse用作IDE,那么您可以在JBoss Tools中安装Hibernate Tools插件。 Hibernate Tools可以将数据库模式反向工程为域模型(实体)类。
要安装它:
为了配置它:
为了使用它:
我尝试在本地数据库中重现您的架构并运行代码生成。
这就是我最终的结果:
<强> Point.java:强>
@Entity
@Table(name = "point", schema = "public")
public class Point implements java.io.Serializable {
private int id;
private City city;
private double latitude;
private double longitude;
public Point() {
}
public Point(int id, City city, double latitude, double longitude) {
this.id = id;
this.city = city;
this.latitude = latitude;
this.longitude = longitude;
}
@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "city", nullable = false)
public City getCity() {
return this.city;
}
public void setCity(City city) {
this.city = city;
}
@Column(name = "latitude", nullable = false, precision = 17, scale = 17)
public double getLatitude() {
return this.latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
@Column(name = "longitude", nullable = false, precision = 17, scale = 17)
public double getLongitude() {
return this.longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
<强> City.java:强>
@Entity
@Table(name = "city", schema = "public")
public class City implements java.io.Serializable {
private String name;
private Cities cities;
private Set<Point> points = new HashSet<Point>(0);
public City() {
}
public City(String name, Cities cities) {
this.name = name;
this.cities = cities;
}
public City(String name, Cities cities, Set<Point> points) {
this.name = name;
this.cities = cities;
this.points = points;
}
@Id
@Column(name = "name", unique = true, nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cities", nullable = false)
public Cities getCities() {
return this.cities;
}
public void setCities(Cities cities) {
this.cities = cities;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "city")
public Set<Point> getPoints() {
return this.points;
}
public void setPoints(Set<Point> points) {
this.points = points;
}
}
<强> Cities.java:强>
@Entity
@Table(name = "cities", schema = "public")
public class Cities implements java.io.Serializable {
private int id;
private Date date;
private Set<City> cities = new HashSet<City>(0);
public Cities() {
}
public Cities(int id, Date date) {
this.id = id;
this.date = date;
}
public Cities(int id, Date date, Set<City> cities) {
this.id = id;
this.date = date;
this.cities = cities;
}
@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false, length = 13)
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "cities")
public Set<City> getCities() {
return this.cities;
}
public void setCities(Set<City> cities) {
this.cities = cities;
}
}