use a onetomany relationship for push info to avoid hibernate automatic lazy loading of onetoone optional relationships

This commit is contained in:
Athou
2013-05-30 10:24:30 +02:00
parent 84a8566af4
commit 0aee6850d5
6 changed files with 16 additions and 13 deletions

View File

@@ -67,7 +67,7 @@ public class FeedDAO extends GenericDAO<Feed> {
List<Feed> feeds = q.getResultList(); List<Feed> feeds = q.getResultList();
for (Feed feed : feeds) { for (Feed feed : feeds) {
FeedPushInfo info = feed.getPushInfo(); FeedPushInfo info = Iterables.getFirst(feed.getPushInfo(), null);
if (info != null) { if (info != null) {
info.getTopic(); info.getTopic();
} }

View File

@@ -29,6 +29,7 @@ import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler; import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedUpdateService; import com.commafeed.backend.services.FeedUpdateService;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.Striped; import com.google.common.util.concurrent.Striped;
@Singleton @Singleton
@@ -157,7 +158,7 @@ public class FeedRefreshUpdater {
} }
private void handlePubSub(final Feed feed) { private void handlePubSub(final Feed feed) {
FeedPushInfo info = feed.getPushInfo(); FeedPushInfo info = Iterables.getFirst(feed.getPushInfo(), null);
if (info != null && info.isActive() == false) { if (info != null && info.isActive() == false) {
new Thread() { new Thread() {
@Override @Override

View File

@@ -18,6 +18,7 @@ import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedPushInfo; import com.commafeed.backend.model.FeedPushInfo;
import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.backend.services.ApplicationSettingsService;
import com.commafeed.backend.services.FeedPushInfoService; import com.commafeed.backend.services.FeedPushInfoService;
import com.google.common.collect.Iterables;
import com.sun.syndication.io.FeedException; import com.sun.syndication.io.FeedException;
public class FeedRefreshWorker { public class FeedRefreshWorker {
@@ -85,7 +86,6 @@ public class FeedRefreshWorker {
private void update(Feed feed) { private void update(Feed feed) {
try { try {
FetchedFeed fetchedFeed = fetcher.fetch(feed.getUrl(), false, FetchedFeed fetchedFeed = fetcher.fetch(feed.getUrl(), false,
feed.getLastModifiedHeader(), feed.getEtagHeader()); feed.getLastModifiedHeader(), feed.getEtagHeader());
@@ -156,11 +156,11 @@ public class FeedRefreshWorker {
topic = "http://" + topic; topic = "http://" + topic;
} }
log.debug("feed {} has pubsub info: {}", feed.getUrl(), topic); log.debug("feed {} has pubsub info: {}", feed.getUrl(), topic);
FeedPushInfo info = feed.getPushInfo(); FeedPushInfo info = Iterables.getFirst(feed.getPushInfo(), null);
if (info == null) { if (info == null) {
info = feedPushInfoService.findOrCreate(feed, hub, topic); info = feedPushInfoService.findOrCreate(feed, hub, topic);
feed.getPushInfo().add(info);
} }
feed.setPushInfo(info);
} }
} }

View File

@@ -9,7 +9,6 @@ import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
@@ -69,9 +68,11 @@ public class Feed extends AbstractModel {
@Column(length = 255) @Column(length = 255)
private String etagHeader; private String etagHeader;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "feed", cascade = { @OneToMany(fetch = FetchType.LAZY, mappedBy = "feed", cascade = {
CascadeType.PERSIST, CascadeType.MERGE }) CascadeType.PERSIST, CascadeType.MERGE })
private FeedPushInfo pushInfo; // use a onetomany relationship for push info to avoid hibernate automatic
// lazy loading of onetoone optional relationships
private Set<FeedPushInfo> pushInfo;
@ManyToMany(mappedBy = "feeds") @ManyToMany(mappedBy = "feeds")
private Set<FeedEntry> entries = Sets.newHashSet(); private Set<FeedEntry> entries = Sets.newHashSet();
@@ -183,11 +184,11 @@ public class Feed extends AbstractModel {
this.lastUpdateSuccess = lastUpdateSuccess; this.lastUpdateSuccess = lastUpdateSuccess;
} }
public FeedPushInfo getPushInfo() { public Set<FeedPushInfo> getPushInfo() {
return pushInfo; return pushInfo;
} }
public void setPushInfo(FeedPushInfo pushInfo) { public void setPushInfo(Set<FeedPushInfo> pushInfo) {
this.pushInfo = pushInfo; this.pushInfo = pushInfo;
} }

View File

@@ -4,7 +4,7 @@ import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.annotations.Index; import org.hibernate.annotations.Index;
@@ -15,7 +15,7 @@ import org.hibernate.annotations.Index;
public class FeedPushInfo extends AbstractModel { public class FeedPushInfo extends AbstractModel {
@JoinColumn(unique = true) @JoinColumn(unique = true)
@OneToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
private Feed feed; private Feed feed;
@Column(length = 2048, nullable = false) @Column(length = 2048, nullable = false)

View File

@@ -23,6 +23,7 @@ import com.commafeed.backend.feeds.FeedUtils;
import com.commafeed.backend.model.Feed; import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedPushInfo; import com.commafeed.backend.model.FeedPushInfo;
import com.commafeed.backend.services.ApplicationSettingsService; import com.commafeed.backend.services.ApplicationSettingsService;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
public class SubscriptionHandler { public class SubscriptionHandler {
@@ -37,7 +38,7 @@ public class SubscriptionHandler {
FeedPushInfoDAO feedPushInfoDAO; FeedPushInfoDAO feedPushInfoDAO;
public void subscribe(Feed feed) { public void subscribe(Feed feed) {
FeedPushInfo info = feed.getPushInfo(); FeedPushInfo info = Iterables.getFirst(feed.getPushInfo(), null);
String hub = info.getHub(); String hub = info.getHub();
String topic = info.getTopic(); String topic = info.getTopic();
String publicUrl = FeedUtils String publicUrl = FeedUtils