Wednesday, March 25, 2015

JPA 2.0 Map Relationships

I need a refreshment of JPA 2.0 support for Maps as I don't run into these type of relationships very often.  The last time I went beyond Sets and Lists was when testing the Metamodel spec in 2010.

The model used consists of a base subclass of a MappedSuperclass that has a unidirectional Map relationship keyed by Key entity to a map of Target entity values.


@MappedSuperclass
public class BaseAbstract {

@Id
@Column(name="base_id")
private Long ident;

public Long getIdent() {
return ident;
}

public void setIdent(Long ident) {
this.ident = ident;
}

}

@Entity
public class Base extends BaseAbstract {
@ManyToMany
private Map<Key, Target> targetMap;

public Map<Key, Target> getTargetMap() {
return targetMap;
}

public void setTargetMap(Map<Key, Target> targetMap) {
this.targetMap = targetMap;
}

}

@Entity
public class Key {

@Id
@Column(name="key_id")
private Long ident;
}

@Entity
public class Target {

@Id
@Column(name="target_id")
private Long ident;
}

DDL results

Hibernate: create table Base (base_id bigint not null, primary key (base_id)) type=InnoDB
Hibernate: create table Base_Target (Base_base_id bigint not null, targetMap_target_id bigint not null, targetMap_KEY bigint not null, primary key (Base_base_id, targetMap_KEY)) type=InnoDB
Hibernate: create table Key (key_id bigint not null, primary key (key_id)) type=InnoDB
Hibernate: create table Target (target_id bigint not null, primary key (target_id)) type=InnoDB
Hibernate: alter table Base_Target add index FKBFB1DEBFB32A10B2 (targetMap_KEY), add constraint FKBFB1DEBFB32A10B2 foreign key (targetMap_KEY) references Key (key_id)
Hibernate: alter table Base_Target add index FKBFB1DEBF8408A9FE (targetMap_target_id), add constraint FKBFB1DEBF8408A9FE foreign key (targetMap_target_id) references Target (target_id)
Hibernate: alter table Base_Target add index FKBFB1DEBF1B8E4DA4 (Base_base_id), add constraint FKBFB1DEBF1B8E4DA4 foreign key (Base_base_id) references Base (base_id)
Hibernate: alter table Base_Target drop foreign key FKBFB1DEBFB32A10B2
Hibernate: alter table Base_Target drop foreign key FKBFB1DEBF8408A9FE
Hibernate: alter table Base_Target drop foreign key FKBFB1DEBF1B8E4DA4

Sorry EclipseLink but Hibernate is used in almost all projects not directly running on WebLogic or the RI version of GlassFish.

No comments:

Total Pageviews

Followers