Featured Post

Event Sourcing Video from Michael Ploed

Event Sourcing I want to share a great video I found few days ago that describes very well what Event Sourcing is.

Friday, September 28, 2012

JPA Set vs List

In this post we'll see the difference on the using of Set and List in a unidirectional association.

List and @JoinTable


Object Point of View

@Entity
public class Parent {
 
@Id
private int parentId;
 
@Column
private String name;

@OneToMany
@JoinTable(name="Parent_Child", joinColumns=@JoinColumn(name="parentId"), inverseJoinColumns=@JoinColumn(name="childId"))
private List<Child> children;

 ...

}


Database Point of View

create table Parent_Child (
        parentId integer not null,
        childId integer not null,
        unique (childId)
)

create table Child (
        childId integer not null auto_increment,
        name varchar(255),
        primary key (childId)
)

create table Parent (
        parentId integer not null auto_increment,
        name varchar(255),
        primary key (parentId)
)

alter table Parent_Child 
        add index FK5AF11D47C916AC84 (parentId), 
        add constraint FK5AF11D47C916AC84 
        foreign key (parentId) 
        references Parent (parentId)

alter table Parent_Child 
        add index FK5AF11D47ABE0291E (childId), 
        add constraint FK5AF11D47ABE0291E 
        foreign key (childId) 
        references Child (childId)



Set and @JoinTable


Object Point of View


@Entity
public class Parent {

@Id
private int parentId;

@Column
private String name;

@OneToMany
@JoinTable(name="Parent_Child", joinColumns=@JoinColumn(name="parentId"), inverseJoinColumns=@JoinColumn(name="childId"))
private Set<Child> children;


...
}


Database Point of View

create table Parent_Child (
        parentId integer not null,
        childId integer not null,
        primary key (parentId, childId),
        unique (childId)
)


create table Child (
        childId integer not null auto_increment,
        name varchar(255),
        primary key (childId)
)


create table Parent (
        parentId integer not null auto_increment,
        name varchar(255),
        primary key (parentId)
)


alter table Parent_Child 
        add index FK5AF11D47C916AC84 (parentId), 
        add constraint FK5AF11D47C916AC84 
        foreign key (parentId) 
        references Parent (parentId)

alter table Parent_Child 
        add index FK5AF11D47ABE0291E (childId), 
        add constraint FK5AF11D47ABE0291E 
        foreign key (childId) 
        references Child (childId)


As you can see the join table Parent_Child has both a primary key both a unique constraint.

So the most important difference is that the conversion into database scripts, preserves the concept of uniqueness of Child entity into the collection associated to a specific Parent.

You cannot associate the same Child object to the same Parent object, as you cannot insert into database two entries with the same couple (Child,Parent) into Parent_Child table.

No comments :

Post a Comment