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

10,410

Solution 1

It's little bit strange to say but that example works without any changes.

I was using Hibernate 4.2 Final version. I don't know why it was not supporting that.

I have changed Version to 3.6.10 Final, and it works. I don't know why Hibernate 4.2 Final was not able to solve that, but this was my solution.

Solution 2

You need to add a discriminator column in your parent-class, and then a discriminator value in each of the child classes. Since you have multiple classes corresponding to the same database table, Hibernate needs to know which row is to be mapped to which class.

So:

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

Now, if you persist an instance of Child1, the database row for this instance will have the value child1 in the column type, so that when you load the row from the database, Hibernate knows that the class of the object to be made based on this row is Child1.

Share:
10,410
Kishan Bheemajiyani
Author by

Kishan Bheemajiyani

information about me

Updated on June 05, 2022

Comments

  • Kishan Bheemajiyani
    Kishan Bheemajiyani almost 2 years

    I am trying to make Single Table Inheritance into the Hibernate, but I am getting an error and I'm not able to figure it out.

    My inherited 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
        }
    
    }
    

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

    and main class method.

    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();
            }
    

    getting this exception.

    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
    

    I have searched a lot but I'm not able to find the solution.

  • Tobb
    Tobb about 9 years
    Do you have such a column in your database?
  • Kishan Bheemajiyani
    Kishan Bheemajiyani about 9 years
    i have put hibernate.hbm2ddl.auto to create so will it create that fields na?
  • Tobb
    Tobb about 9 years
    It should, but the first rule for finding errors is "never trust anything". You should check if it is actually present, as the error message claims it is not.
  • Kishan Bheemajiyani
    Kishan Bheemajiyani about 9 years
    ya i know it should. but one thing is that i have put that annotation in top so saying. i have seen a lot of example it sould work.