简体   繁体   English

休眠:字段“ id”没有默认值

[英]Hibernate: Field 'id' doesn't have a default value

I have a web app which has a database that has a Customer entity and a Car entity. 我有一个Web应用程序,该应用程序的数据库包含一个Customer实体和一个Car实体。 I have a ManyToMany relationship between Car and Customer (or at least I intent to) and It's in my BookedCar class. 我在CarCustomer之间(或者至少是我打算这样做)之间存在ManyToMany关系,并且在我的BookedCar类中。

BookedCar : BookedCar

@Entity
@Table(name = "booked_cars")
data class BookedCar(@Column(name = "car_id") var carId: Int = 0,
            @Column(name = "customer_id") var customerId: Int = 0,
            @Column var startDate: Date = Date(),
            @Column var endDate: Date = Date()) {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0
}

Car : Car

@Entity
@Table(name = "cars")
data class Car(@Column var name: String = "",
           @Column var price: Int = 0,
           @Column(length = 1000) var imgURL: String = "") {

    fun getPriceForPeriodPerDay(days: Int) = when (days) {
        in 0..3 -> this.price
        in 3..8 -> this.price - 7
        in 8..15 -> this.price - 15
        in 15..Int.MAX_VALUE -> this.price - 20
        else -> this.price
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    @ManyToMany(cascade = [CascadeType.ALL])
    @JoinTable(name = "booked_cars",
        joinColumns = [(JoinColumn(name = "car_id", referencedColumnName = "id"))],
        inverseJoinColumns = [JoinColumn(name = "customer_id", referencedColumnName = "id")])
    lateinit var customers: Set<Customer>
}

Customer : Customer

@Entity
@Table(name = "customers")
data class Customer(@Column var phoneNumber: String = "",
                @Column var name: String = "") {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int = 0

    @ManyToMany(mappedBy = "customers")
    lateinit var bookedCars: Set<Car>
}

Problem: 问题:

In one of my Controllers I have a function with some mapping and code: 在我的一个控制器中,我有一个带有一些映射和代码的函数:

...
val customer = this.customerRepository.findOneByPhoneNumber(phoneNumber)
            ?: Customer(phoneNumber, "")
val car = this.carRepository.findOne(id)
val bookedCar = BookedCar(car.id,customer.id, startDate, endDate)
this.requestedCarRepository.saveAndFlush(bookedCar)
...

The problem comes exactly on this line 问题恰好在这条线上

this.requestedCarRepository.saveAndFlush(bookedCar)

and the error is 错误

java.sql.SQLException: Field 'id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) ~[mysql-connector-java-5.1.45.jar:5.1.45]
...
at com.rentautosofia.rentacar.controller.FrontCarController.orderProcess(FrontCarController.kt:113) ~[classes/:na]
...

I don't think I wrote the JoinColumn s properly. 我认为我没有正确编写JoinColumn Overall any help would be highly appreciated. 总体而言,任何帮助将不胜感激。

Note: 注意:

I tried recreateing the databases. 我尝试重新创建数据库。

spring.datasource.url = jdbc:mysql://localhost:3306/rentacar?...&createDatabaseIfNotExist=true

Edit 1: 编辑1:

Jpa DDL auto-increment: Jpa DDL自动递增:

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
# Using "create" will delete and recreate the tables every time the project is started
spring.jpa.hibernate.ddl-auto = update

You can't mix join table with entity 您不能将联接表与实体混合

You defined BookedCar as an entity ... do you really need this or you just need a join table to do the many to many join between customer and car ... if you need more columns to be put in the booked car (not the mandatory ID, I mean a column that holds some business) then ok make it an entity, otherwise just make it a join column 您将BookedCar定义为实体...您是否真的需要这个实体?还是只需要一个BookedCar表来进行客户与汽车之间的多对多BookedCar ...如果您需要在预订的汽车中放入更多列(而不是必填ID,我的意思是说一列保存一些业务),然后将其设为实体,否则将其设为连接列

If you decided to make it an entity, then you will need one to many relation from each of customer and car to booked_car, and a many to one from booked_car to each of them .... instead of this ManyToMany relation that you are using in car and customer 如果您决定使其成为一个实体,那么您将需要从每个客户和汽车到booked_car的一对多关系,以及从booked_car到他们每个的多对一关系....而不是您正在使用的ManyToMany关系在汽车和客户中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM