简体   繁体   中英

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. I have a ManyToMany relationship between Car and Customer (or at least I intent to) and It's in my BookedCar class.

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 :

@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 :

@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. 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:

Jpa DDL auto-increment:

# 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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