【问题标题】:Unique rows in OnetoOne relation case . How to prevent duplicates?OnetoOne 关系案例中的唯一行。如何防止重复?
【发布时间】:2019-08-12 16:09:38
【问题描述】:

我对代码有疑问。我希望行是唯一的。对于飞行,我只使用@UniqueConstraint 注释,它工作正常。

但是,在酒店的情况下,他将副本写入我的数据库。

我怀疑这是由于表是通过一对一关系连接的。

我保证你必须在飞行舱里写,而不是在旅馆里写。

@Entity
@Component
@Table(name = "Flight", uniqueConstraints = @UniqueConstraint(columnNames = {"departure","currency","destination","price","flightnumber","airline","departuretime","returntime","expiresAt","userName","airlineIata" }))
public class Flight {

    private Long id;

    private String departure;

    private String currency;

    private String destination;

...等

@Entity
@Component
@Table(name = "Hotel", uniqueConstraints = @UniqueConstraint(columnNames = {"site","rating","phone","name","longitude","latitude","image3","image2","image","hotelLink","describe","currency","category","address","price" }))

public class Hotel {

    private Long id;
    private String name;
    private String currency;
    private String site;
    private BigDecimal price;
    private String address;
    private Double rating;
    private String image;
    private String image2;
    private String image3;
    private String hotelLink;
    private String phone;
    private String describe;
    private String latitude;
    private String longitude;
    private String category;

getter ...和setter ...

some code....

  hotel.setPrice(hotelCurrencyService.amountCalculator(array.getVendor3Price(), exchangeRate));

flight.setDeparture(flightDTO.getDeparture()); 一些代码.....

 flight.setHotel(hotel);
 flightRepository.save(flight);

【问题讨论】:

    标签: java database spring hibernate jpa


    【解决方案1】:

    这家酒店是以前保存的吗?还是保存方法保留两个实体(航班和酒店)? 在我的项目中,我会尝试从数据库中获取对象,然后,如果对象已经存在,我会实施更新或中止规则。

    【讨论】:

    • 保存方法同时保留两个实体
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2021-10-19
    • 2021-10-24
    相关资源
    最近更新 更多