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

时间:2015-02-02 10:43:21

标签: java hibernate jdbc single-table-inheritance mssql-jdbc

我正在尝试将单表继承引入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

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

2 个答案:

答案 0 :(得分:1)

您需要在父类中添加一个discriminator列,然后在每个子类中添加一个discriminator值。由于您有多个类对应于同一个数据库表,因此Hibernate需要知道哪个行要映射到哪个类。

所以:

@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的实例,则此实例的数据库行将在child1列中具有值type,这样当您从数据库加载行时,Hibernate知道基于此行创建的对象的类是Child1

答案 1 :(得分:0)

说起来有点奇怪,但这个例子没有任何变化。

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

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