简体   繁体   中英

HQL Update issue

I have two entities. Data_mart and entity_data. entity_data extends data_mart. I have id column in both the data_mart and entity_data. data_id in entity mart is the foregin key of the data_mart(Pkey over there is 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

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. It should have been data_id.

I could not figure out how this is taken??

You must not have an ID in the subclass. It extends the base class, which already has an 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.

See the dcumentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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