I did some more digging into my <discriminator> problem.
I have the following table in MySQL:
Code:
create table if not exists coverage
(
coverage_id int unsigned not null auto_increment,
coverage_type varchar(20) not null,
coverage_subtype varchar(20) not null;
plan_id int unsigned not null,
primary key(coverage_id)
)
type=innodb
min_rows=0
max_rows=1000
pack_keys=default
row_format=default;
I populate it with the following data:
Code:
insert into coverage(coverage_type, coverage_subtype, plan_id) select 'std', 'standard', plan_id from plan where plan_type = 'employee';
insert into coverage(coverage_type, coverage_subtype, plan_id) select 'std', 'nonstandard', plan_id from plan where plan_type = 'dependent';
insert into coverage(coverage_type, coverage_subtype, plan_id) select 'ltd', 'standard', plan_id from plan where plan_type = 'employee';
insert into coverage(coverage_type, coverage_subtype, plan_id) select 'ltd', 'nonstandard', plan_id from plan where plan_type = 'dependent';
I can run the following query in the MySQL query tool and get back good results:
Code:
select
case
when coverage_type = "std" and coverage_subtype = "standard" then "STANDARD-STD"
when coverage_type = "ltd" and coverage_subtype = "standard" then "STANDARD-LTD"
when coverage_type = "std" and coverage_subtype = "nonstandard" then "NON-STANDARD-STD"
when coverage_type = "ltd" and coverage_subtype = "nonstandard" then "NON-STANDARD-LTD"
end
from
coverage
where
coverage_id = 1;
But when I put this <discriminator> into my Hibernate properties:
Code:
<class name="Coverage"
table="COVERAGE"
polymorphism="implicit"
discriminator-value="abstract">
<id name="id" column="COVERAGE_ID" type="long">
<generator class="native"/>
</id>
<discriminator type="string">
<formula>
case
when coverage_type = "std" and coverage_subtype = "standard" then "STANDARD-STD"
when coverage_type = "ltd" and coverage_subtype = "standard" then "STANDARD-LTD"
when coverage_type = "std" and coverage_subtype = "nonstandard" then "NON-STANDARD-STD"
when coverage_type = "ltd" and coverage_subtype = "nonstandard" then "NON-STANDARD-LTD"
end
</formula>
</discriminator>
<!--
<set name="rates" outer-join="true" inverse="true" cascade="all-delete-orphan">
<key column="coverage_id"/>
<one-to-many class="Rate"/>
</set>
<one-to-one name="plan" class="Plan" cascade="save-update"/>
-->
<subclass name="StandardSTDCoverage" discriminator-value="STANDARD-STD"/>
<subclass name="StandardLTDCoverage" discriminator-value="STANDARD-LTD"/>
<subclass name="NonStandardSTDCoverage" discriminator-value="NON-STANDARD-STD"/>
<subclass name="NonStandardLTDCoverage" discriminator-value="NON-STANDARD-LTD"/>
</class>
I get a SQL syntax exception from Hibernate:
Code:
Hibernate: select coverage0_.COVERAGE_ID as COVERAGE1_3_,
coverage0_.case
when coverage0_.coverage_type = 'std' and coverage0_.coverage_subtype = 'standard' then 'STANDARD-STD'
when coverage0_.coverage_type = 'ltd' and coverage0_.coverage_subtype = 'standard' then 'STANDARD-LTD'
when coverage0_.coverage_type = 'std' and coverage0_.coverage_subtype = 'nonstandard' then 'NON-STANDARD-STD'
else 'NON-STANDARD-LTD'
coverage0_.end
as clazz_3_, rates1_.coverage_id as coverage4___, rates1_.RATE_ID as RATE1___, rates1_.RATE_ID as RATE1_0_, rates1_.PERCENTAGE as PERCENTAGE2_0_, rates1_.COVERAGE_ID as COVERAGE3_2_0_, coverage2_.COVERAGE_ID as COVERAGE1_1_,
coverage2_.case
when coverage2_.coverage_type = 'std' and coverage2_.coverage_subtype = 'standard' then 'STANDARD-STD'
when coverage2_.coverage_type = 'ltd' and coverage2_.coverage_subtype = 'standard' then 'STANDARD-LTD'
when coverage2_.coverage_type = 'std' and coverage2_.coverage_subtype = 'nonstandard' then 'NON-STANDARD-STD'
else 'NON-STANDARD-LTD'
coverage2_.end
as clazz_1_, plan3_.PLAN_ID as PLAN1_2_, plan3_.PLAN_TYPE as PLAN2_2_ from COVERAGE coverage0_ left outer join RATE rates1_ on coverage0_.COVERAGE_ID=rates1_.coverage_id left outer join COVERAGE coverage2_ on rates1_.COVERAGE_ID=coverage2_.COVERAGE_ID left outer join PLAN plan3_ on coverage0_.COVERAGE_ID=plan3_.PLAN_ID where coverage0_.COVERAGE_ID=?
May 12, 2005 10:57:48 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1064, SQLState: 42000
May 12, 2005 10:57:48 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Syntax error or access violation, message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'when coverage0_.coverage_type = 'std' and coverage0_.coverage_s"
May 12, 2005 10:57:48 AM org.hibernate.event.def.DefaultLoadEventListener onLoad
INFO: Error performing load command
org.hibernate.exception.SQLGrammarException: could not load an entity: [twodeep.model.Coverage#1]
I turned on "show_sql" so I can see the generated SQL. I thought I'd turned off the 1:m relationship with Rate.
I'd be grateful to anyone who can see what I've done wrong with the <formula> SQL. Thanks - %