Wednesday, March 25, 2015

JPA 2.0 Map Relationships

see http://wiki.obrienlabs.cloud/display/DEV/Object+Relational+Mapping
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.

1 comment:

zaidepachl said...

Bet365 Casino and Sportsbook app for iOS and Android - JamBase
춘천 출장샵 en-us › mobile › en-us › mobile Bet365 Casino and Sportsbook apps for iOS and Android. Bet365 Casino and Sportsbook app for iOS 경상남도 출장샵 and 평택 출장마사지 Android. App. bet365 Casino and Sportsbook app for iOS and Android. App. bet365 화성 출장안마 Bonus Code: LFB100C-B Bet365 Sportsbook is live, bet365 Promo Code: LFB100C-B Bet365 Signup Bonus: 김해 출장안마 Get 100% up to $1000 Bonus 100 C Rating: 3.8 7 votes

Total Pageviews

Followers