Cork Kochi
Cork Kochi

Reputation: 1891

Extend an entity class in JPA throws Unknown column 'DTYPE' in 'field list' error

@Entity
@Table(name = "PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)  
public class Person {    
    @Id
    @GeneratedValue
    @Column(name = "PERSON_ID")
    private Long personId;

    @Column(name = "FIRSTNAME")
    private String firstname;

    @Column(name = "LASTNAME")
    private String lastname;

    // Constructors and Getter/Setter methods, 
}

Employee class extends Person

@Entity    
public class Employee extends Person {

    @Transient
    private Date joiningDate;   


    // Copy Constructors and Getter/Setter methods, 
}

The Employee class has only a transient object So I am not sure about using of @DiscriminatorColumn and @DiscriminatorValue, When i tried without using Discriminator that throws error

session.save(employee);

I am trying to save Employee object that throws Unknown column 'DTYPE' in 'field list' error

Upvotes: 14

Views: 17813

Answers (4)

Rohit Kumar
Rohit Kumar

Reputation: 1341

D_TYPE is basically name of your child class.

Let's suppose you have:

  • Abstract Employee(id, name);
  • class FullTimeEmployee extends Employee(Double salary)
  • with Inheritence.SINGLE_TABLE strategy.

A table employee will be created with these columns

Employee (D_TYPE, id, name, salary)

where D_TYPE will be name of your child classes.

employeeRepository.insert(Employee emp);

where you can pass emp as new FullTimeEmployee(id, name, salary);

After insert it will automatically populate d_type as Full_Time_Employee

If you want to change the name of D_TYPE column use @DiscriminatorColumn(name="customizedNameofD_TYPE")

Just FYI: mostly used strategy is InheritanceType.JOINED

Upvotes: 0

Gaurav Saraf
Gaurav Saraf

Reputation: 23

Persist child entity rather than persisting parent entity.

@MappedSuperclass
public class Person { 

And

@Entity
@Table(name = "PERSON")
public class Employee extends Person {

This should resolve your issue.

Upvotes: -1

schlebe
schlebe

Reputation: 3715

I have encountered the same issue using EclipseLink and I have tried to use @discriminator annotation ... without success because in this solution, a specific column must be added in 2 tables.

I don't have the possibility to add columns in tables, so I have found following solution :

@Entity
@Table(name = "PERSON")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public class Person {    

I have just changed inheritance type from SINGLE_TABLE to TABLE_PER_CLASS

I have found useful explanation on Thoughts on Java

Just for information, my 2 tables are in reality 1 table and a view on same table with 2 or 3 joins.

Upvotes: 6

You should use @DiscriminatorColumn and @DiscriminatorValue. You can find a good example here.

Upvotes: 3

Related Questions