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.
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
.