当前位置: 首页 > 知识库问答 >
问题:

JPA持久性积垢沉积。save()语法错误

贝德辉
2023-03-14

我正在开发一个航空公司管理系统,通过JPA Crudepository方法访问mysql数据库。每当我试图使用存储库将飞行对象保存到数据库中时。保存(flightObject),它会引发以下错误-

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, capacity, manufacturer, model, plane, yearofmanufacture, price, seatsleft,' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)

我的航班创建功能如下-

    @RequestMapping(method = RequestMethod.POST, value = "/flight/{flight_number}")
        public Flight createUpdateFlight(@PathVariable(value = "flight_number") String flightNumber, @RequestParam(value="price") int price, @RequestParam(value="from") String from, @RequestParam(value="to") String to, @RequestParam(value="departuretime") String departuretime, @RequestParam(value="arrivaltime") String arrivaltime,@RequestParam(value="seatsleft") int seatsleft, @RequestParam("description") String description, @RequestParam(value="capacity") int capacity, @RequestParam(value="manufacturer") String manufacturer,@RequestParam(value="model") String model,@RequestParam(value="yearofmanufacture") String yearofmanufacture) throws ParseException {

            DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
            Date arrivaldate = format.parse(arrivaltime);
            Date departuredate = format.parse(departuretime);

            Plane plane = new Plane(capacity,model,manufacturer,yearofmanufacture);

            Flight flight = new Flight(flightNumber, price, from, to, departuredate, arrivaldate, seatsleft, description, plane, null);

            try{
                flightRepository.save(flight);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            return flightRepository.findBynumber(flight.getNumber());
        }

我的航班存储库类-

@Repository
@Transactional
public interface FlightRepository extends CrudRepository<Flight, String> {
    Flight findBynumber(String number);
}

我的飞行实体类如下-

@Entity
@Table(name = "flight")
@XmlRootElement
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Flight {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "number")
    private String number; // Each flight has a unique flight number.

    @Column(name = "price")
    private int price;

    @Column(name = "from")
    private String from;

    @Column(name = "to")
    private String to;

    @Column(name = "departuretime")
    private Date departureTime;

    @Column(name = "arrivaltime")
    private Date arrivalTime;

    @Column(name = "description")
    private String description;

    @Column(name = "seatsleft")
    private int seatsLeft;


    //@OneToOne(fetch=FetchType.LAZY)
    //@JoinColumn(name="plane")
    @Embedded
    private Plane plane; // Embedded


    @OneToMany()
    @JoinTable(
              name="passenger_list",
              joinColumns=
                { @JoinColumn(name="number") },//, referencedColumnName="number"),
              inverseJoinColumns=
                 @JoinColumn(name="passenger" , referencedColumnName="id" ))
    private List<Passenger> passengers;


    public Flight(String number, int price, String from, String to, Date departureTime, Date arrivalTime,
            int seatsLeft, String description, Plane plane, List<Passenger> passengers) {
        super();
        System.out.println("Inside Flight constr");
        this.number = number;
        this.price = price;
        this.from = from;
        this.to = to;
        this.departureTime = departureTime;
        this.arrivalTime = arrivalTime;
        this.description = description;
        this.seatsLeft = seatsLeft;
        this.plane = plane;
        this.passengers = passengers;
    }


    public Flight()
    {
        super();
    }

    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String getTo() {
        return to;
    }
    public void setTo(String to) {
        this.to = to;
    }
    public Date getDepartureTime() {
        return departureTime;
    }
    public void setDepartureTime(Date departureTime) {
        this.departureTime = departureTime;
    }
    public Date getArrivalTime() {
        return arrivalTime;
    }
    public void setArrivalTime(Date arrivalTime) {
        this.arrivalTime = arrivalTime;
    }
    public int getSeatsLeft() {
        return seatsLeft;
    }
    public void setSeatsLeft(int seatsLeft) {
        this.seatsLeft = seatsLeft;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Plane getPlane() {
        return plane;
    }
    public void setPlane(Plane plane) {
        this.plane = plane;
    }

    public List<Passenger> getPassengers() {
        return passengers;
    }
    public void setPassengers(List<Passenger> passengers) {
        this.passengers = passengers;
    }
}

我的可嵌入平面类如下-

@Embeddable
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Plane implements Serializable{

    //@Id
    @Column(name = "plane")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int plane;

    @Column(name = "capacity")
    private int capacity;

    @Column(name = "model")
    private String model;

    @Column(name = "manufacturer")
    private String manufacturer;

    @Column(name = "yearofmanufacture")
    private String yearofmanufacture;


    public Plane(int capacity, String model, String manufacturer, String yearofmanufacture) {
        super();
        System.out.println("Inside Plane constr");
        this.capacity = capacity;
        this.model = model;
        this.manufacturer = manufacturer;
        this.yearofmanufacture = yearofmanufacture;
    }

    public Plane() {
        super();
    }

    public int getId() {
        return plane;
    }

    public void setId(int id) {
        this.plane = id;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getYearofmanufacture() {
        return yearofmanufacture;
    }

    public void setYearofmanufacture(String yearofmanufacture) {
        this.yearofmanufacture = yearofmanufacture;
    }

}

这是我的数据库架构图像-

如果你能发现任何错误,请告诉我。我已经试了一段时间了,但没有成功。我们可以打印JPA查询以在执行之前检查语法吗?

共有3个答案

柯子琪
2023-03-14

试试这个

@RequestMapping(method = RequestMethod.POST, value = "/flight/{flight_number}")
public Flight createUpdateFlight(@PathVariable(value = "flight_number") String flightNumber, @RequestParam(value="price") int price, @RequestParam(value="from") String from, @RequestParam(value="to") String to, @RequestParam(value="departuretime") String departuretime, @RequestParam(value="arrivaltime") String arrivaltime,@RequestParam(value="seatsleft") int seatsleft, @RequestParam("description") String description, @RequestParam(value="capacity") int capacity, @RequestParam(value="manufacturer") String manufacturer,@RequestParam(value="model") String model,@RequestParam(value="yearofmanufacture") String yearofmanufacture) throws ParseException {

    DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
    Date arrivaldate = format.parse(arrivaltime);
    Date departuredate = format.parse(departuretime);

    Plane plane = new Plane(capacity,model,manufacturer,yearofmanufacture);

    Flight flight = new Flight(flightNumber, price, from, to, departuredate, arrivaldate, seatsleft, description, plane, null);

    try{
        flight = flightRepository.save(flight); //save() method returns a Flight entity after saving it.
    }
    catch(Exception e){
        e.printStackTrace();
    }

    return flight;
}

PS:我还没有测试过这个。

赵元白
2023-03-14

很高兴看到你的FlightRepository结构。我认为“findBynumber”方法中有一个错误,从语法上讲,它不能转换为写查询。它应该看起来像“findByNumber”。

刁俊人
2023-03-14

问题已经解决。问题在于“from”和“to”属性“from”和“to”是数据库关键字,因此不能直接用作JPA实体中的列名。在查询中直接使用它们会使其无效。更改列名成功了。另一种选择是在编写查询时将列名括在引号中,这样它们就不会被视为关键字。

 类似资料:
  • 这就是我的datasourcebean的样子

  • 在下面的代码中,我无法设置某些产品类别和产品标签: 代码位于我的函数中。php文件: 产品是用正确的和创建的。但我对产品类别和产品标签一无所获: 编辑:我已将此代码移动到文件,它可以工作。

  • /**若要更改此许可证标头,请在项目属性中选择许可证标头。*要更改此模板文件,请选择工具模板*并在编辑器中打开模板。*/ Edit:如果我使用saveAndFlush()强制写入,我将得到no transaction的错误:org.springframework.dao.invalidDataAccessapiusageException:no transaction正在进行;嵌套异常是javax

  • 个性积木赋予程小奔”人格“魅力,让它会笑会闹会撒娇。 添加个性积木 1. 在”设备“下,选中”程小奔“。点击积木区最下方的”+“按钮。 2. 在弹出的”扩展中心“页面,选择”个性“,点击”+“。 3. 返回编辑页。瞧,积木多了一种类型:个性。 使用个性积木 让我们来试试新的个性积木吧。我们来制作一个小游戏,程小奔无意中走进一个山洞,洞里都是金币,它非常开心地将所有金币收入囊中。 开始编程前,要先将

  • 我正在设计一个页面,它将有一个工具,搜索和显示结果到一个带有分页的表中,用户可以自由选择每页要显示的行数。 控制器包含以下功能: 设置和管理。php 意见如下: 公司php 当我输入行数并按下按钮时,它返回所需的分页,但页面链接数不等于所需的。e、 g.如果搜索只返回一行,它应该显示在表中,没有任何页面链接。但在我的例子中,它显示的是单行(搜索结果)和所有页面链接(等于总行数/每页)。 这可能是因

  • 问题内容: JPA中的和批注有什么区别?它们可以一起使用吗? 如果 他们可以一起使用吗?还是其中之一就足够了? 问题答案: 表示要保留属性,并且要使用标准映射。它具有允许您指定是否要延迟加载属性以及该属性是否为空的参数。 允许您指定数据库中属性要保留到的列的名称。 如果您指定一个不带另一个,那么您将获得明智的默认行为,因此,除了特殊情况外,通常人们只使用一个。 因此,如果我们想要延迟加载属性并指定