我正在尝试将单表继承引入 Hibernate,但出现错误,我无法弄清楚。
我继承的班级
@Entity
@Table(name = "Vechicle")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DTYPE")
public class Vehicle_object implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int vehicleid;
@Column(name = "Vehiclename")
private String vehiclename;
public Vehicle_object() {
super();
// TODO Auto-generated constructor stub
}
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;
}
}
儿童班。
@Entity
public class TwoWheeler extends Vehicle_object {
private String handle;
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
public TwoWheeler() {
super();
// TODO Auto-generated constructor stub
}
}
第二个孩子。
@Entity
public class FourWheeler extends Vehicle_object {
private String stearing;
public String getStearing() {
return stearing;
}
public void setStearing(String stearing) {
this.stearing = stearing;
}
public FourWheeler() {
super();
// TODO Auto-generated constructor stub
}
}
和主类方法。
try {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Vehicle_object object = new Vehicle_object();
//object.setVehicleid(0);
object.setVehiclename("Car");
TwoWheeler twoWheeler = new TwoWheeler();
twoWheeler.setVehiclename("Apache");
twoWheeler.setHandle("Apache handle");
FourWheeler fourWheeler = new FourWheeler();
fourWheeler.setStearing("Ford Car Stearing");
fourWheeler.setVehiclename("FIGO FORD");
session.save(object);
session.save(twoWheeler);
session.save(fourWheeler);
session.getTransaction().commit();
session.clear();
session.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
得到这个例外。
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'DTYPE'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:306)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186)
... 23 more
我已经搜索了很多,但我无法找到解决方案。