Entity inheritance an error while refreshing with EntityManager

96 views Asked by At

When using entity inheritance hierarchy witch @DiscriminatorColumn storing DiscriminatorType.STRING values in MySQL ENUM. Below code example:

    @Entity
    @DiscriminatorColumn(name ="account_type", discriminatorType = DiscriminatorType.STRING,
         columnDefinition = "ENUM('natural_person', 'firm', 'provider', 'employee', 'administrator', 'moderator', 'user')")
@DiscriminatorValue("user")
public class User implements Serializable { ... } 

and inherited entity:

@Entity 
@DiscriminatorValue("firm")
public class Firm extends User { ... } 

when I create Firm object or removes everything works ok, even when I find it with EntityManager, but if I make EnityManager.refresh(firm) than there is an error complaining about:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'firm0_.account_type' in 'field list'

UPDATE:

When I changed this columnDefinition of @DescriminatorColumn by deleting it and storing strings "firm", "user", ERROR ALSO OCCURES!

UPDATE 2:

I have a little suggestion that as Firm extends UserAccount this discriminator column account_type should be in user_account table. So error firm0.account_type seems stupid as it search this column in user_account.account_type! I have another subclass Person and it persists and than saves OK, but Firm only persists, removes but DON'T REFRESH!

generated column: user_account

UPDATE 3:

Found sql log like this:

    Hibernate: 
    insert 
    into
        user_account
        (activation_code, email, last_failed_login, last_logged, login, password, registration_date, account_type) 
    values
        (?, ?, ?, ?, ?, ?, ?, 'firm')
Hibernate: 
    insert 
    into
        firm
        (address_city, address_country, address_office_no, address_building_no, address_state, address_street, address_zip_code, client_id, company_number, name, phone_number, skype_name, statistic_number, vatin, user_id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    select
        firm0_.user_id as user_id2_14_0_,
        firm0_1_.activation_code as activati3_14_0_,
        firm0_1_.email as email4_14_0_,
        firm0_1_.last_failed_login as last_fai5_14_0_,
        firm0_1_.last_logged as last_log6_14_0_,
        firm0_1_.login as login7_14_0_,
        firm0_1_.password as password8_14_0_,
        firm0_1_.registration_date as registra9_14_0_,
        firm0_.address_city as address_1_3_0_,
        firm0_.address_country as address_2_3_0_,
        firm0_.address_office_no as address_3_3_0_,
        firm0_.address_building_no as address_4_3_0_,
        firm0_.address_state as address_5_3_0_,
        firm0_.address_zip_code as address_7_3_0_,
        firm0_.client_id as client_15_3_0_,
        firm0_.company_number as company_8_3_0_,
        firm0_.name as name9_3_0_,
        firm0_.skype_name as skype_n11_3_0_,
        firm0_.statistic_number as statist12_3_0_,
        firm0_.vatin as vatin13_3_0_,
        firm0_2_.corporation_id as corporat5_8_0_,
        firm0_2_.description as descript1_8_0_,
        firm0_2_.name as name2_8_0_,
        firm0_2_.type as type3_8_0_,
        firm0_.account_type as account_1_14_0_ 
    from
        firm firm0_ 
    inner join
        user_account firm0_1_ 
            on firm0_.user_id=firm0_1_.user_id 
    left outer join
        provider firm0_2_ 
            on firm0_.user_id=firm0_2_.provider_id 
    where
        firm0_.user_id=?
Jun 10, 2015 5:37:16 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Jun 10, 2015 5:37:16 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'firm0_.account_type' in 'field list'
Jun 10, 2015 5:37:16 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool
2

There are 2 answers

0
Michał Ziobro On

Ok It seems I found solution but it is odd, and I will be testes it later.

private String accountType; 

@Column(name = "account_type", insertable = false, updatable = false)
public String getAccountType() {
    return accountType;
}

public void setAccountType(String accountType) {
    this.accountType = accountType;
}

I have added this code into UserAccount (superclass - root of inheritance hierarchy), where discriminator column is defined... BUT it seems odd behaviour to define explicite discriminator column in entity! I read that it is anit-pattern, not recommanded to add, and moreover to use such discriminator column in code...

0
MarkOfHall On

I'm guessing that when JPA generates the DDL it is using the columnDefinition string as a literal for the column definition. Thus, it should be "account_type ENUM('natural_person', 'firm', 'provider', 'employee', 'administrator', 'moderator', 'user')"