gpt4 book ai didi

java - hibernate 时冗余数据多对一映射

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:07 25 4
gpt4 key购买 nike

我有 2 个表:Trip 和 Place(多对一),我的问题是,当我添加 2 个具有不同数据但相同地点的行程时,它会向 Trip 表添加 2 条记录,向 Place 表添加 2 条记录, 而它应该只将一条记录添加到 Place。

例如,我有 2 次不同日期的旅行,但它们都在同一个地方 - 意大利、罗马。因此,这些数据应该只有一个记录:意大利、罗马。

如何在我的应用程序中避免此类行为?

旅行等级:

public class Trip implements java.io.Serializable { 
private int idTrip;
private int idHotel;
private Date date;
private int cost;
private int profit;
private String organisator;
private int period;
private String food;
private String transport;
private int persons;
private int kidsAmount;

private String ownerName;
private String ownerLastName;

private Place place;

+ 构造函数、get() 和 set() 方法,

地方类:

public class Place implements java.io.Serializable {

private int idPlace;
private String country;
private String city;
private String island;
private String information;
private Set<Trip> trips;

+ 构造函数、get() 和 set() 方法,

行程映射文件:

<hibernate-mapping>
<class name="pl.th.java.biuro.hibernate.Trip" table="Trip">
<id column="idTrip" name="idTrip" type="int">
<generator class="native"/>
</id>
<property column="date" name="date" type="date"/>
<property column="cost" name="cost" type="int"/>
<property column="profit" name="profit" type="int"/>
<property column="organisator" name="organisator" type="string"/>
<property column="period" name="period" type="int"/>
<property column="food" name="food" type="string"/>
<property column="transport" name="transport" type="string"/>
<property column="persons" name="persons" type="int"/>
<property column="kidsAmount" name="kidsAmount" type="int"/>
<property column="idHotel" name="idHotel" type="int"/>
<many-to-one fetch="select" name="place" class="pl.th.java.biuro.hibernate.Place">
<column name="idPlace" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>

放置映射文件:

<hibernate-mapping>
<class name="pl.th.java.biuro.hibernate.Place" table="Place">
<id column="idPlace" name="idPlace" type="int">
<generator class="native"/>
</id>
<property column="country" name="country" type="string"/>
<property column="city" name="city" type="string"/>
<property column="island" name="island" type="string"/>
<property column="information" name="information" type="string"/>
<set name="trips" table="Trip" inverse="true" lazy="true" fetch="select">
<key>
<column name="idPlace" not-null="true" />
</key>
<one-to-many class="pl.th.java.biuro.hibernate.Trip" />
</set>
</class>
</hibernate-mapping>

我还用我的 MySQL 数据库添加了一些屏幕截图,也许有一些问题不允许我正确地执行此操作: MySQL database

编辑:在 Place 映射文件和 Set in Place 类中添加了一对多关系,但仍然遇到同样的问题。

EDIT2:将具有持久实体的代码添加到数据库中:

Session session = DatabaseConnection.getFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();

trip.setPlace(place);
session.save(place);
session.save(trip);

tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
System.out.println("Exception found while adding new trip: " + e.getMessage());
e.printStackTrace();
} finally {
session.close();
}

但我仍然遇到同样的问题......我添加了一个地点 A 的行程,然后在下一步中我添加了另一个相同的行程,这就是我得到的: Duplicate places

EDIT3: 创建 Trip 和 Place 对象:

Trip trip = null;
Place place = null;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateInString = tripDateField.getText();
java.util.Date date = null;
try {
date = sdf.parse(dateInString);
place = new Place(tripCountryField.getText(), tripCityField.getText(), tripIslandField.getText(), tripPlaceInfoTextArea.getText());
int period = 0, persons = 0, kidsAmount = 0;

//W razie braku niewymaganych liczbowych danych ustawiane są wartości -1
if (tripPeriodField.getText().equals("")) {
period = -1;
}
if (tripPersonsField.getText().equals("")) {
persons = -1;
}
if (tripKidsAmountField.getText().equals("")) {
kidsAmount = -1;
}

trip = new Trip(new Date(date.getTime()), Integer.parseInt(tripCostField.getText()), Integer.parseInt(tripProfitField.getText()),
tripOrganisatorField.getText(), period, tripFoodField.getText(), tripTransportField.getText(),
persons, kidsAmount, tripClientNameField.getText(), tripClientLastNameField.getText());

} catch (ParseException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
try {
date = sdf.parse("0000-00-00");
} catch (ParseException ex1) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex1);
}
} catch (NumberFormatException e) {
place = new Place("111", "111", "111", "111");
trip = new Trip(null, WIDTH, WIDTH, dateInString, WIDTH, dateInString, dateInString, WIDTH, ABORT, dateInString, dateInString);

System.out.println("Exception while getting trip / place data: " + e.toString());
}

dataEdition.addTrip(trip, place, dataEdition.validateTripData(trip, place), addRemoveSearchTripDialog);

我从 textFields 获取这些对象数据,并在需要时将它们解析为 int,所以我想这应该没问题。在此之后,我将这 2 个对象传递到另一个方法中,我将它们保存到数据库中。

最佳答案

看起来您在每次提交时都创建了一个位置。

Place place = new Place(tripCountryField.getText(), tripCityField.getText(), tripIslandField.getText(), tripPlaceInfoTextArea.getText());

然后您期望 Hibernate 以某种方式神奇地确定您实际上可能想要使用数据库中的现有条目。

这行不通。

如果提交的地点已经存在,那么您需要以某种方式加载现有的持久实体并使用它。

您可以通过允许用户选择现有地点并通过该地点的 ID 发送来在前端解决此问题,或者查询匹配的地点关于表单提交。

例如

Place place = myDatabaseService.findPlace(country, name ...) //does not account for misspellings

if(place == null){
place = new Place(tripCountryField.getText(), tripCityField.getText(), tripIslandField.getText(), tripPlaceInfoTextArea.getText());
}

关于java - hibernate 时冗余数据多对一映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com