ID 是在将车辆对象持久化到数据库时自动生成的。如何在不持久化的情况下获得 ID 的值?
这是代码:
public class Vehicle{
@Id
@GeneratedValue
int id;
private int year;
private String model;
//getters, setters
}
更新
public void updateVehicle(Vehicle vehicle) {
em.getTransaction().begin();
Vehicle vehicleDB = em.find(Vehicle.class, vehicle.getId());
vehicleDB.setYear(vehicle.getYear());
vehicleDB.setModel(vehicle.getModel());
em.getTransaction().commit();
}