Hi,
I believe I have a very common and simple question but anyway I cannot find an answer on it.
I have a table per hierarchy strategy with a joined secondary table.
Code:
@javax.persistence.Entity(name="Card")
@javax.persistence.Table(name="Card")
@javax.persistence.DiscriminatorColumn(name="type")
@javax.persistence.Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Card
{
@javax.persistence.Id
Integer id;
@javax.persistence.Basic
String type;
@javax.perisistence.Basic
Integer status;
}
@javax.persistence.Entity(name="PhyCard")
@javax.persistence.DiscriminatorValue("Phycard")
@javax.persistence.SecondaryTable{name="PhyCard",
pkJoinColumns={@javax.persistence.PrimaryKeyJoinColumn(name="id")}
)
public class PhyCard extends Card
{
@javax.persistence.Basic
Integer bw;
}
It generated tables:
Code:
create table Card{
id integer,
type varchar,
status integer,
primary key(id) };
create table PhyCard{
id integer,
bw integer,
primary key(id) };
I successfully created the tables and inserted data there.
Now, I want to update PhyCard (subclass) using hql query:
Code:
update PhyCard set status=1 where id=1;
I expected it to generate a SQL query like
Code:
update Card set status=1 where id=1;
since the field status belongs to Card, not to PhyCard. Why should Hibernate access PhyCard at all? The query could also have an additional predicate for extra validation:
Code:
update Card set status=1 where id=1 and type='PhyCard';
But Hibernate started to do something different. It generated query:
Code:
insert into HT_Card select card.id as id from Card card left outer join PhyCard phycard on card.id=phycard.id where type='UspCard' and id=1;
(I changed the aliases used by Hibernate to make the query more readable)
Why Hibernate needs to join Card and PhyCard in this case I don't know,
but the main problem is that the SQL query fails since id in the where clause is ambiguous. The same field 'id' belongs to two tables.
I tried to alias it in the original query:
Code:
update PhyCard as p set status=1 where p.id=1;
Hibernate stripped the alias from the query and failed for the same reason.
What do I do wrong? I could not find the answer neither in the documentation nor in the forum.
Thank you,
yevgeny