2

我正在使用 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)

4

2 回答 2

8

如果使用 TABLE_PER_CLASS,则必须使用此 ID 生成策略:@GeneratedValue(strategy = GenerationType.TABLE)

于 2012-05-07T17:28:59.450 回答
0

车辆.java

@Entity
@Table(name="vehicle")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

public class Vehicle 
{

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    @Column(name="vehicle_id")
    private int vehicleId;
    @Column(name="vehicle_name")
    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;
    }

}
于 2016-06-30T08:37:46.377 回答