简体   繁体   English

HQL更新问题

[英]HQL Update issue

I have two entities. 我有两个实体。 Data_mart and entity_data. Data_mart和entity_data。 entity_data extends data_mart. entity_data扩展了data_mart。 I have id column in both the data_mart and entity_data. 我在data_mart和entity_data中都有id列。 data_id in entity mart is the foregin key of the data_mart(Pkey over there is id). 实体市场中的data_id是data_mart的前向键(那里的Pkey是id)。

@Entity
@Table(name = "data_mart")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type")
public class DataMaster {

/** The id. */
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(unique = true, nullable = false)
private Long id;
/** The type. */
private String type;

/** The created by. */
private String createdBy;
.....
 }

@Entity
@Table(name = "entity_data")
@PrimaryKeyJoinColumn(name = "dataId")
@DiscriminatorValue("FILE")
@Where(clause = "active = '1'")
public class EntityData extends DataMaster {

/** The data id. */
 @Column(insertable = false, updatable = false)
    private Long dataId;

/** The delimiter. */
private String delimiter;
....
 }

And the table structure of the child table is as follows 子表的表结构如下

CREATE TABLE entity_data (
id bigint(20) NOT NULL AUTO_INCREMENT,
data_id bigint(20) NOT NULL, 
delimiter varchar(20) DEFAULT NULL,
file_path `varchar(200) DEFAULT NULL,
...
)

When i try to update the table entity_data with an HQL query, below is the intermediate query formed by the Hibernate 当我尝试使用HQL查询更新表entity_data时,以下是由Hibernate形成的中间查询

create temporary table if not exists HT_entity_data (id bigint not null);
insert into HT_entity_data
select entitydata0_.data_id as data_id from entity_data 
entitydata0_ inner join data_master entitydata0_1_ on   
 entitydata0_.data_id=entitydata0_1_.id where entitydata0_.file_path='abcdd'


 update entity_data set file_path='new_path' 
 where (id) IN  (select data_id from HT_entity_data)

The above query is giving wrong results. 上面的查询给出了错误的结果。 in the where clause of the above update statement, id is coming. 在以上更新语句的where子句中,id即将到来。 It should have been data_id. 应该是data_id。

I could not figure out how this is taken?? 我不知道如何采取?

You must not have an ID in the subclass. 子类中不得包含ID。 It extends the base class, which already has an ID. 它扩展了已经具有ID的基类。 In the JOINED inheritance strategy, the primary key of the table associated with the subclass is also a foreign key to the primary key of the table associated with the base class. JOINED继承策略中,与子类关联的表的主键也是与基类关联的表的主键的外键。

See the dcumentation . 参见文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM