我正在尝试使用以下代码持久保存实体
public ReservationEntity persistReservation(ReservationRequest reservationRequest) {
ReservationEntity re = new ReservationEntity();
re.setTotalPrice(new BigDecimal(123));
re.setFlightInstance(getFlightInstanceEntity(reservationRequest.getFlightId()));
List<PassengerEntity> passengers = new ArrayList<>();
for (Passenger p : reservationRequest.getPassengers()){
PassengerEntity pe = new PassengerEntity();
pe.setFirstName(p.getFirstName());
pe.setLastName(p.getLastName());
passengers.add(pe);
}
re.setPassenger(passengers);
em.getTransaction().begin();
em.persist(re);
em.getTransaction().commit();
return re;
}
乘客实体
@Entity
@Table(name = "passenger", schema = "", catalog = "dbname")
public class PassengerEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator="passengerId")
private int passengerId;
private String firstName;
private String lastName;
// GETTERS-SETTERS
}
@Entity
@Table(name = "reservation", schema = "", catalog = "dbname")
public class ReservationEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "reservationId")
private int reservationId;
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name="passengerId")
private List<PassengerEntity> passenger;
public void setPassenger(List<PassengerEntity> passenger){this.passenger = passenger;}
public List<PassengerEntity> getPassenger(){return this.passenger;}
我尝试使用GenerationType.AUTO
和GenerationType.Sequence
,但在这两种情况下,我都收到table PassengerEntity.SEQUENCE doesn't exist
(JPA EclipseLink DatabaseException: 'table foo.SEQUENCE doesn't exist')
我尝试添加这些行,它实际上用数据删除了我的整个架构......
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.create-ddl-jdbc-file-name" value="createDDL_ddlGeneration.jdbc"/>
<property name="eclipselink.drop-ddl-jdbc-file-name" value="dropDDL_ddlGeneration.jdbc"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
这是我特定表的架构。该表有一个FK,它有同样的问题。我试图先坚持父母,然后是孩子。
CREATE TABLE `passenger`(
`passengerId` INT NOT NULL AUTO_INCREMENT,
`firstName` VARCHAR(45) NOT NULL,
`LastName` VARCHAR(45) NOT NULL,
`reservationId` INT NOT NULL,
PRIMARY KEY (`passengerId`),
INDEX `reservationId_idx` (`reservationId` ASC),
CONSTRAINT `passenger_reservation_reservationId`
FOREIGN KEY (`reservationId`)
REFERENCES `reservation`(`reservationId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
如果我将GenerationType
更改为GenerationType.IDENTITY
,我会收到错误
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Field 'reservationId' doesn't have a default value
Error Code: 1364
Call: INSERT INTO dbname.passenger (FIRSTNAME, LASTNAME) VALUES (?, ?)
bind => [2 parameters bound]
Query: InsertObjectQuery(com.project.entity.PassengerEntity@994)
将generator
添加到实体并将类型更改为SEQUENCE
后,我收到同样的错误
Internal Exception: java.sql.SQLException: Field 'reservationId' doesn't have a default value
答案 0 :(得分:0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="columnName")
private int passengerId;
尝试用上面的代码注释你的passengerId