gpt4 book ai didi

java - Hibernate 一对一映射问题 - 级联功能不起作用

转载 作者:行者123 更新时间:2023-11-30 10:25:23 24 4
gpt4 key购买 nike

我有以下 HBM 实体类:

  1. Train.java、Ticket.java、Booking.java、Passenger.java

我在 Booking 和 Ticket 之间有一对一关系。请在下面找到 hibernate pojo 类:

@Entity
@Table(name = "TICKET_MASTER")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Ticket implements Serializable {

@Transient
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "TICKET_SEQ")
@SequenceGenerator(name = "TICKET_SEQ", sequenceName = "TICKET_SEQ", allocationSize = 1)
@Column(name = "TICKET_ID", nullable = false)
private Long ticketId;

@Column(name = "TICKET_NO", nullable = false)
private String ticketNo;

@OneToOne(mappedBy = "ticket",cascade=CascadeType.ALL)
private Booking booking;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "ticket")
private Set<Passenger> passengers;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "TRAIN_ID", nullable = false)
private Train train;

//getters & setters
}

@Entity
@Table(name = "BOOKING_MASTER")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Booking extends RMSEntity {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "BOOKING_SEQ")
@SequenceGenerator(name = "BOOKING_SEQ", sequenceName = "BOOKING_SEQ", allocationSize = 1)
@Column(name = "BOOKING_ID", nullable = false)
private Long bookingId;

@Column(name = "BOOKING_NO", nullable = false)
private String bookingNo;

@OneToOne
@JoinColumn(name = "TICKET_ID", referencedColumnName = "TICKET_ID")
private Ticket ticket;

//gettres & setters
}

由于上述两个类之间出现问题,我将省略 Passenger 和 Train 的 pojo 类。

请在下面找到我的服务层代码:

@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
@Override
public void saveBookingDetails(PassengerDTO passengerDTO) {
BeanUtils.copyProperties(passengerDTO, passenger);*/
Train train = getDaoProvider().getTrainDao().find(passengerDTO.getTrainId());
Ticket ticket = new Ticket();
ticket.setTicketId((long) (Math.random() * 100));
String ticketNo = UUID.randomUUID().toString().split("-")[0];
ticket.setTicketNo(ticketNo);
Booking booking = new Booking();
String bookingNo = UUID.randomUUID().toString().split("-")[0];
booking.setBookingId((long)(Math.random() * 100));
booking.setBookingNo(bookingNo);
ticket.setBooking(booking);
ticket.setTrain(train);
getDaoProvider().getTicketDao().saveOrUpdate(ticket);
}

我遇到以下异常:

Caused By: org.hibernate.exception.ConstraintViolationException: could not execute batch
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:119)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:97)
Truncated. see log file for complete stacktrace
Caused By: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("SYSTEM"."TICKET_MASTER"."BOOKING_ID")

at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:12296)
at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:246)
at weblogic.jdbc.wrapper.PreparedStatement.executeBatch(PreparedStatement.java:197)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:110)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.doExecuteBatch(BatchingBatch.java:97)
Truncated. see log file for complete stacktrace

我正在使用 Spring-Hibernate ORM 功能。

最佳答案

在双向关系中,你应该有一个从每一方到另一方的引用,你错过了在预订中注入(inject)车票,火车也是如此。

Booking booking = new Booking();
booking.setTicket(ticket);
train.setTicket(ticket);
String bookingNo = UUID.randomUUID().toString().split("-")[0];

另一件事,因为您在预订中的 id 是自动生成的,所以您不应该设置 id,hibernate 会根据持久性负责为其分配 id

String bookingNo = UUID.randomUUID().toString().split("-")[0];// this line should be removed
booking.setBookingId((long)(Math.random() * 100));//this line should be removed
ticket.setTicketId((long) (Math.random() * 100));//this line should be removed

同时删除 Ticket 的线路设置 ID。

此外,由于您正在使用序列生成,因此您需要将类型指定为序列而不是自动

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BOOKING_SEQ")

关于java - Hibernate 一对一映射问题 - 级联功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261315/

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