gpt4 book ai didi

java - 无法持久化对象

转载 作者:行者123 更新时间:2023-11-29 21:05:00 25 4
gpt4 key购买 nike

我正在尝试使用以下代码持久保存一个实体

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.AUTOGenerationType.Sequence,在这两种情况下,我都收到了 table PassengerEntity.SEQUENCE 没有的错误存在 ( 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 也有同样的问题。我试图首先坚持 parent ,然后是 child 。

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

最佳答案

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="columnName")
private int passengerId;

尝试用上面的代码注释你的passengerId

关于java - 无法持久化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36898737/

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