com.microsoft.sqlserver.jdbc.SQLServerException: 列名无效 'DTYPE'

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'DTYPE'

我正在尝试将 Single Table 继承到 Hibernate 中,但出现错误,我无法弄清楚。

我的继承class

@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;
}

}

Child class.

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

}

第二个child.

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

}

和主要 class 方法。

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

我搜索了很多,但找不到解决方案。

您需要在父 class 中添加一个鉴别器列,然后在每个子 class 中添加一个鉴别器值。由于您有多个 classes 对应于同一个数据库 table,Hibernate 需要知道哪一行要映射到哪个 class.

所以:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Parent { //bla }


@Entity
@DiscriminatorValue("child1")
public class Child1 extends Parent { //bla }

@Entity
@DiscriminatorValue("child2")
public class Child2 extends Parent { //bla }

现在,如果您保留 Child1 的一个实例,该实例的数据库行将在列 type 中具有值 child1,这样当您从数据库加载该行时, Hibernate知道根据这一行要做的对象的class是Child1.

说起来有点奇怪,但该示例无需任何更改即可运行。

我使用的是 Hibernate 4.2 Final 版本。我不知道为什么它不支持。

我已将版本更改为 3.6.10 Final,并且可以使用。我不知道为什么 Hibernate 4.2 Final 无法解决这个问题,但这是我的解决方案。