JPA关联问题

JPA的PO代码片段如下:

父类:
[code="java"]
@Entity
@Table(name = "message_t")
@Inheritance(strategy = InheritanceType.JOINED)
public class Message implements Serializable {
[/code]

子类:
[code="java"]
@Entity
@Table(name = "draw_message_t")
@PrimaryKeyJoinColumn(referencedColumnName = "iwoid", name = "message_oid")
public class DrawMessage extends Message implements Serializable {
[/code]

子类有多个,此处略。

在查询父类Message 时,生成的sql会自动关联子类
sql:
[code="java"]
Hibernate:
select
message0_.iwoid as iwoid2_,
message0_.broadcast_type as broadcast2_2_,
message0_.content as content2_,
message0_.create_time as create4_2_,
message0_.creator as creator2_,
message0_.invalid_time as invalid6_2_,
message0_.modifier as modifier2_,
message0_.modify_time as modify8_2_,
message0_.msg_time as msg9_2_,
message0_.msg_type as msg10_2_,
message0_.remark as remark2_,
message0_.sender_oid as sender12_2_,
message0_.status as status2_,
message0_.title as title2_,
message0_1_.channel_name as channel1_5_,
message0_1_.channel_oid as channel2_5_,
message0_2_.draw_issue as draw1_6_,
message0_2_.draw_time as draw2_6_,
message0_2_.game_type as game3_6_,
message0_3_.apply_status as apply1_7_,
message0_4_.earn_type as earn1_8_,
message0_4_.game_id as game2_8_,
message0_4_.game_name as game3_8_,
message0_4_.game_oid as game4_8_,
message0_5_.ranking_year as ranking1_9_,
message0_5_.week_of_year as week2_9_,
message0_6_.ranking_date as ranking1_10_,
case
when message0_1_.message_oid is not null then 1
when message0_2_.message_oid is not null then 2
when message0_3_.message_oid is not null then 3
when message0_4_.message_oid is not null then 4
when message0_5_.message_oid is not null then 5
when message0_6_.message_oid is not null then 6
when message0_.iwoid is not null then 0
end as clazz_
from
message_t message0_
left outer join
busi_dynamic_t message0_1_
on message0_.iwoid=message0_1_.message_oid
left outer join
draw_message_t message0_2_
on message0_.iwoid=message0_2_.message_oid
[/code]

如果不想关联,还是通过JPAQL该怎么做

1个回答

@Inheritance(strategy = InheritanceType.JOINED)
这个策略改掉为如下的注解:
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
这两种方式各有优缺点。single-table查询效率高,浪费表空间

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问