我正在使用 Hibernate 框架 3.6.10.Final 和 MySql。我正进入(状态
线程“主”org.hibernate.MappingException 中的异常:无法使用标识列密钥生成与映射:org.koushik.javabrains.dto.Vehicle
当我包括@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
在车辆类中。没有这个注释它工作正常。
Vehicle.java
是我的基类:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Vehicle
{
@Id
@GeneratedValue
private int vehicleId;
private String vehicleName;
public int getVehicleId() {
return vehicleId;
}
public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;
}
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
}
TwoWheeler.java
:
@Entity
public class TwoWheeler extends Vehicle {
private String SteeringHandle;
public String getSteeringHandle() {
return SteeringHandle;
}
public void setSteeringHandle(String steeringHandle) {
SteeringHandle = steeringHandle;
}
}
FourWheeler.java
:
@Entity
public class FourWheeler extends Vehicle {
private String SteeringWheel;
public String getSteeringWheel() {
return SteeringWheel;
}
public void setSteeringWheel(String steeringHandle) {
SteeringWheel = steeringHandle;
}
}
我的主要课程:
public class HibernateTest {
public static void main(String[] args)
{
Vehicle vehicle = new Vehicle();
vehicle.setVehicleName("audi"+(int)(Math.random() * 100) + 1);
TwoWheeler bike = new TwoWheeler();
bike.setVehicleName("bike");
bike.setSteeringHandle("Bike SteeringHandle");
FourWheeler car = new FourWheeler();
car.setVehicleName("car");
car.setSteeringWheel("Car SteeringHandle");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(vehicle);
session.save(bike);
session.save(car);
session.getTransaction().commit();
session.close();
}
}
运行时出现以下错误:
SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:参见http://www.slf4j.org/codes.html#StaticLoggerBinder了解更多详情。线程“主”org.hibernate.MappingException 中的异常:无法使用标识列密钥生成与映射:org.koushik.javabrains.dto.Vehicle at org.hibernate.persister.entity.UnionSubclassEntityPersister.(UnionSubclassEntityPersister.java:90) at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:90) 在 org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:286) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872) 在org.koushik.hibernate.HibernateTest.main(HibernateTest.java:26)