使用实体继承层次结构时,@DiscriminatorColumn在MySQL ENUM中存储DiscriminatorType.STRING值。下面的代码示例:
@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 { ... }
和继承的实体:
@Entity
@DiscriminatorValue("firm")
public class Firm extends User { ... }
当我创建Firm对象或删除所有对象时,即使我使用EntityManager找到它也可以,但是如果我使EnityManager.refresh(firm)出现,则会报错:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'firm0_.account_type' in 'field list'
更新:
当我通过删除@DescriminatorColumn的columnDefinition并存储字符串“ firm”,“ user”,还发生错误时,就改变了它!
更新2:
我有一点建议,随着公司扩展UserAccount,该标识符列account_type应该在user_account表中。因此,错误firm0.account_type看起来很愚蠢,因为它在user_account.account_type中搜索此列!
我还有另一个子类Person,它可以持久保存,并且可以保存,但是Firm只可以持久保存,可以删除但不要刷新!
更新3:
找到这样的sql日志:
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
最佳答案
我猜想,当JPA生成DDL时,它使用columnDefinition字符串作为列定义的文字。因此,应为"account_type ENUM('natural_person', 'firm', 'provider', 'employee', 'administrator', 'moderator', 'user')"
关于mysql - 使用EntityManager刷新时,实体继承发生错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30758318/