如何处理JPA多对一关系?
我正在设计一个用于收集天气数据的应用程序。我有 2 个 POJO 对象“位置”和“记录”。 Location包含有关经纬度和当前天气状况的信息,Record包含特定位置一段时间内的所有天气信息,因此与Location具有多对一关系。我的类的定义如下:
Location.java
@Entity
@Table(name = "location")
@NamedQueries( {
@NamedQuery(name = "findLocations", query = "SELECT e FROM Location e ORDER BY e.longitude, e.latitude"),
@NamedQuery(name = "findLocationByLatLong", query = "SELECT e from Location e WHERE e.latitude = :latitude AND e.longitude = :longitude"),
@NamedQuery(name = "findLocationById", query = "SELECT e from Location e WHERE e.id = :id"),
@NamedQuery(name = "deleteLocationById", query= "DELETE Location e WHERE e.id = :id"),
@NamedQuery(name = "updateLocation", query = "UPDATE Location e SET e.lastModifiedDate = :lastModifiedDate WHERE e.id = :id")})
public class Location implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
@Column(name="latitude", nullable=false)
protected String latitude;
@Column(name="longitude", nullable=false)
protected String longitude;
@Column(name="lastModifiedDate")
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;
@Column(name="windDirection")
private float windDirection;
@Column(name="windSpeed")
private float windSpeed;
@Column(name="temperature")
private float temperature;
}
和 Record.java
@Entity
@Table(name = "weatherdata")
@NamedQueries( {
@NamedQuery(name = "findWeatherRecordById", query = "SELECT e from Record e WHERE e.id = :id"),
@NamedQuery(name = "findWeatherRecords", query = "SELECT e from Record e WHERE e.parent = :parent") })
public class Record implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
protected Long id;
@Column(name="mTime")
@Temporal(TemporalType.TIMESTAMP)
private Date mtime;
@Column(name="windDirection")
private float windDirection;
@Column(name="windSpeed")
private float windSpeed;
@Column(name="temperature")
private float temperature;
@ManyToOne(cascade = { CascadeType.ALL }, targetEntity=Location.class, fetch = FetchType.EAGER)
@JoinColumn(name="locationId")
protected Location parent;
}
我传入的数据采用以下形式:
latitude,longitude,date,winddirection,windspeed,temperature
36.9822,-122.0153,20100907000000.00,158,2.68,20.57
38.1838,-120.54,20100907000000.00,248,0,26.68
38.3495,-121.9688,20100907000000.00,149,0.45,33.9
38.41935,-121.36029,20100907000000.00,322,0.9,33.9
37.91617,-122.286,20100907000000.00,224,0,24.46
38.587,-121.3162,20100907000000.00,315,0,34.46
36.8717,-121.6555,20100907000000.00,294,3.13,18.34
现在,每当我获得一条记录时,我想将其插入到 Record 表中。由于我有 Location 的外键,因此我还将添加 Location 表的 locationId。 另一件事是,位置表没有预先填充。因此,每当有新记录出现时,我首先将其插入位置表中,然后使用外键填充记录表。我不想在位置表中出现重复的位置条目。如您所见,位置表还将包含最新的温度、风速和风向数据。
我正在使用以下代码来完成此操作:
Location loc = handler.getLocation(line);
//loc.setTemperature(0);
Location dbLoc = null;
try {
Query q = eManager.createNamedQuery("findLocationByLatLong");
q.setParameter("latitude", loc.getLatitude());
q.setParameter("longitude", loc.getLongitude());
dbLoc = (Location) q.getSingleResult();
} catch (Exception e) {
System.out.println("Location not found! Creating new location");
Logger.getLogger("WeatherRecorderBean.class").log(Level.WARNING, e.getMessage());
}
Record r = handler.getRecord(line);
if(dbLoc!=null) {
r.setParent(dbLoc);
dbLoc.setLastModifiedDate(r.getMtime());//I am doing this so as to know what time the weather change entry is about
dbLoc.setWindDirection(r.getWindDirection());
dbLoc.setWindSpeed(r.getWindSpeed());
dbLoc.setTemperature(r.getTemperature());
eManager.merge(r);
}
else {
dbLoc = new Location();
dbLoc.setLatitude(loc.getLatitude());
dbLoc.setLongitude(loc.getLongitude());
//eManager.persist(dbLoc);
r.setParent(dbLoc);
dbLoc.setLastModifiedDate(r.getMtime());
dbLoc.setWindDirection(r.getWindDirection());
dbLoc.setWindSpeed(r.getWindSpeed());
dbLoc.setTemperature(r.getTemperature());
eManager.merge(r);
//eManager.merge(dbLoc);
}
但是通过这样做,发生的情况是位置被重复。这意味着我在位置表中有多个相同经度和纬度但具有不同温度、风速数据的条目。我想要完成的是一个纬度和经度的单个条目,并使用最新数据更新风速、温度和风向字段。
请帮忙!
I am designing an application for collecting weather data. I have 2 POJO objects "Location" and "Record". Location contains information about latitude and longitude and the current weather conditions, and Record contains all the weather information over time for a specific location thus having a Many-to-one relation with Location. The definition of the classes I have is as follows:
Location.java
@Entity
@Table(name = "location")
@NamedQueries( {
@NamedQuery(name = "findLocations", query = "SELECT e FROM Location e ORDER BY e.longitude, e.latitude"),
@NamedQuery(name = "findLocationByLatLong", query = "SELECT e from Location e WHERE e.latitude = :latitude AND e.longitude = :longitude"),
@NamedQuery(name = "findLocationById", query = "SELECT e from Location e WHERE e.id = :id"),
@NamedQuery(name = "deleteLocationById", query= "DELETE Location e WHERE e.id = :id"),
@NamedQuery(name = "updateLocation", query = "UPDATE Location e SET e.lastModifiedDate = :lastModifiedDate WHERE e.id = :id")})
public class Location implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
@Column(name="latitude", nullable=false)
protected String latitude;
@Column(name="longitude", nullable=false)
protected String longitude;
@Column(name="lastModifiedDate")
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;
@Column(name="windDirection")
private float windDirection;
@Column(name="windSpeed")
private float windSpeed;
@Column(name="temperature")
private float temperature;
}
And Record.java
@Entity
@Table(name = "weatherdata")
@NamedQueries( {
@NamedQuery(name = "findWeatherRecordById", query = "SELECT e from Record e WHERE e.id = :id"),
@NamedQuery(name = "findWeatherRecords", query = "SELECT e from Record e WHERE e.parent = :parent") })
public class Record implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
protected Long id;
@Column(name="mTime")
@Temporal(TemporalType.TIMESTAMP)
private Date mtime;
@Column(name="windDirection")
private float windDirection;
@Column(name="windSpeed")
private float windSpeed;
@Column(name="temperature")
private float temperature;
@ManyToOne(cascade = { CascadeType.ALL }, targetEntity=Location.class, fetch = FetchType.EAGER)
@JoinColumn(name="locationId")
protected Location parent;
}
And my incoming data is in the form of:
latitude,longitude,date,winddirection,windspeed,temperature
36.9822,-122.0153,20100907000000.00,158,2.68,20.57
38.1838,-120.54,20100907000000.00,248,0,26.68
38.3495,-121.9688,20100907000000.00,149,0.45,33.9
38.41935,-121.36029,20100907000000.00,322,0.9,33.9
37.91617,-122.286,20100907000000.00,224,0,24.46
38.587,-121.3162,20100907000000.00,315,0,34.46
36.8717,-121.6555,20100907000000.00,294,3.13,18.34
Now whenever I get a record, I want to insert it in Record table. And as I have a foreign key to Location, I will also add the locationId of Location table.
Another thing, Location table is not prepopulated. So whenever a new record comes I first insert it in Location table, and then populate the Record table with the foreign key. And I dont want duplication location entries in Location table. Location table will also contain the most latest temperature, windspeed and winddirection data as you can see.
I am using the following code to accomplish that:
Location loc = handler.getLocation(line);
//loc.setTemperature(0);
Location dbLoc = null;
try {
Query q = eManager.createNamedQuery("findLocationByLatLong");
q.setParameter("latitude", loc.getLatitude());
q.setParameter("longitude", loc.getLongitude());
dbLoc = (Location) q.getSingleResult();
} catch (Exception e) {
System.out.println("Location not found! Creating new location");
Logger.getLogger("WeatherRecorderBean.class").log(Level.WARNING, e.getMessage());
}
Record r = handler.getRecord(line);
if(dbLoc!=null) {
r.setParent(dbLoc);
dbLoc.setLastModifiedDate(r.getMtime());//I am doing this so as to know what time the weather change entry is about
dbLoc.setWindDirection(r.getWindDirection());
dbLoc.setWindSpeed(r.getWindSpeed());
dbLoc.setTemperature(r.getTemperature());
eManager.merge(r);
}
else {
dbLoc = new Location();
dbLoc.setLatitude(loc.getLatitude());
dbLoc.setLongitude(loc.getLongitude());
//eManager.persist(dbLoc);
r.setParent(dbLoc);
dbLoc.setLastModifiedDate(r.getMtime());
dbLoc.setWindDirection(r.getWindDirection());
dbLoc.setWindSpeed(r.getWindSpeed());
dbLoc.setTemperature(r.getTemperature());
eManager.merge(r);
//eManager.merge(dbLoc);
}
But by doing this, what is happening is Locations are being duplicated. Meaning I have multiple entries for same longitude and latitude but with different temperature, windspeed data in the Location table. What I want to accomplish is have a single entry for one latitude and longitude and update the windspeed, temperature and winddirection fields with the latest data.
Please HELP!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将
记录
中的ALL
操作级联到位置
,因此当您合并新的记录
时和< /strong> 其新的父Location
,无需再次合并瞬态Location
(否则您将得到重复的行)。我在下面的代码中添加了一些注释(我没有修复所有问题,IMO 还有更多问题,但建议的更改应该至少删除重复
Location
条目的创建):You're cascading
ALL
operations fromRecord
toLocation
so when you merge a newRecord
and its new parentLocation
, there is no need to merge a transientLocation
again (or you'll get duplicate lines).I've put some comments in your code below (I didn't fix everything, there are IMO more problems but the suggested changes should at least remove the creation of duplicates
Location
entries):已解决:-)
我创建了一个用于保存
位置
的新函数。我也遇到了一些同步问题。我在 MDB
onMessage()
函数中有这个函数。因此,在一个onMessage()
完成之前,另一个 onMessage() 已开始,因此会创建重复的条目!希望这对将来的人有帮助!
Solved :-)
I create a new function for saving
Location
.I also had a few synchronization problems. I had this function in a MDB
onMessage()
function. So before oneonMessage()
was finished another one started hence creating duplicate entries!Hope this helps somebody in future!