materialize the manytomany relationship to have better control over queries

This commit is contained in:
Athou
2013-07-04 23:40:25 +02:00
parent d57a1c9e2a
commit 4efa18f9e6
8 changed files with 110 additions and 70 deletions

View File

@@ -4,11 +4,10 @@ import java.util.Date;
import java.util.Set;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@@ -17,8 +16,6 @@ import javax.persistence.Transient;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.google.common.collect.Sets;
@Entity
@Table(name = "FEEDS")
@SuppressWarnings("serial")
@@ -111,8 +108,8 @@ public class Feed extends AbstractModel {
@Column(length = 40)
private String lastContentHash;
@ManyToMany(mappedBy = "feeds")
private Set<FeedEntry> entries = Sets.newHashSet();
@OneToMany(mappedBy = "feed", cascade = CascadeType.REMOVE)
private Set<FeedFeedEntry> entryRelationships;
@OneToMany(mappedBy = "feed")
private Set<FeedSubscription> subscriptions;
@@ -145,13 +142,6 @@ public class Feed extends AbstractModel {
@Transient
private boolean urgent;
@PreRemove
private void removeEntriesFromFeed() {
for (FeedEntry entry : entries) {
entry.getFeeds().remove(this);
}
}
public Feed() {
}
@@ -176,14 +166,6 @@ public class Feed extends AbstractModel {
this.lastUpdated = lastUpdated;
}
public Set<FeedEntry> getEntries() {
return entries;
}
public void setEntries(Set<FeedEntry> entries) {
this.entries = entries;
}
public String getMessage() {
return message;
}
@@ -344,4 +326,12 @@ public class Feed extends AbstractModel {
this.normalizedUrlHash = normalizedUrlHash;
}
public Set<FeedFeedEntry> getEntryRelationships() {
return entryRelationships;
}
public void setEntryRelationships(Set<FeedFeedEntry> entryRelationships) {
this.entryRelationships = entryRelationships;
}
}

View File

@@ -9,8 +9,6 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@@ -20,8 +18,6 @@ import javax.persistence.TemporalType;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.google.api.client.util.Sets;
@Entity
@Table(name = "FEEDENTRIES")
@SuppressWarnings("serial")
@@ -35,9 +31,8 @@ public class FeedEntry extends AbstractModel {
@Column(length = 40, nullable = false)
private String guidHash;
@ManyToMany
@JoinTable(name = "FEED_FEEDENTRIES", joinColumns = { @JoinColumn(name = "FEEDENTRY_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "FEED_ID", nullable = false, updatable = false) })
private Set<Feed> feeds = Sets.newHashSet();
@OneToMany(mappedBy = "entry", cascade = CascadeType.REMOVE)
private Set<FeedFeedEntry> feedRelationships;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@JoinColumn(nullable = false, updatable = false)
@@ -82,14 +77,6 @@ public class FeedEntry extends AbstractModel {
this.updated = updated;
}
public Set<Feed> getFeeds() {
return feeds;
}
public void setFeeds(Set<Feed> feeds) {
this.feeds = feeds;
}
public Set<FeedEntryStatus> getStatuses() {
return statuses;
}
@@ -130,4 +117,12 @@ public class FeedEntry extends AbstractModel {
this.author = author;
}
public Set<FeedFeedEntry> getFeedRelationships() {
return feedRelationships;
}
public void setFeedRelationships(Set<FeedFeedEntry> feedRelationships) {
this.feedRelationships = feedRelationships;
}
}

View File

@@ -0,0 +1,57 @@
package com.commafeed.backend.model;
import java.io.Serializable;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@Table(name = "FEED_FEEDENTRIES")
@SuppressWarnings("serial")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class FeedFeedEntry implements Serializable {
@Id
@ManyToOne
@JoinColumn(name = "FEED_ID")
private Feed feed;
@Id
@ManyToOne
@JoinColumn(name = "FEEDENTRY_ID")
private FeedEntry entry;
public FeedFeedEntry() {
}
public FeedFeedEntry(Feed feed, FeedEntry entry) {
this.feed = feed;
this.entry = entry;
}
public Feed getFeed() {
return feed;
}
public void setFeed(Feed feed) {
this.feed = feed;
}
public FeedEntry getEntry() {
return entry;
}
public void setEntry(FeedEntry entry) {
this.entry = entry;
}
}