remove lazy loading

This commit is contained in:
Jeremie Panzer
2013-03-26 11:36:31 +01:00
parent 8f1061a8b2
commit e5e40dac23
6 changed files with 53 additions and 48 deletions

View File

@@ -5,7 +5,6 @@ import java.util.Set;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
@@ -27,7 +26,7 @@ public class Feed extends AbstractModel {
@Column(length = 1024) @Column(length = 1024)
private String message; private String message;
@OneToMany(mappedBy = "feed", fetch = FetchType.EAGER) @OneToMany(mappedBy = "feed")
private Set<FeedEntry> entries = Sets.newHashSet(); private Set<FeedEntry> entries = Sets.newHashSet();
public Feed() { public Feed() {

View File

@@ -4,7 +4,6 @@ import java.util.Set;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
@@ -25,8 +24,8 @@ public class FeedCategory extends AbstractModel {
@ManyToOne @ManyToOne
private FeedCategory parent; private FeedCategory parent;
@OneToMany(mappedBy = "category", fetch = FetchType.EAGER) @OneToMany(mappedBy = "category")
private Set<FeedSubscription> subscriptions = Sets.newHashSet(); private Set<FeedSubscription> subscriptions;
public String getName() { public String getName() {
return name; return name;
@@ -53,6 +52,9 @@ public class FeedCategory extends AbstractModel {
} }
public Set<FeedSubscription> getSubscriptions() { public Set<FeedSubscription> getSubscriptions() {
if (subscriptions == null) {
return Sets.newHashSet();
}
return subscriptions; return subscriptions;
} }

View File

@@ -4,6 +4,7 @@ import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Lob; import javax.persistence.Lob;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
@@ -18,7 +19,7 @@ public class FeedEntry extends AbstractModel {
@Column(length = 2048) @Column(length = 2048)
private String guid; private String guid;
@ManyToOne @ManyToOne(fetch = FetchType.EAGER)
private Feed feed; private Feed feed;
@Column(length = 2048) @Column(length = 2048)

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.model;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
@@ -10,10 +11,10 @@ import javax.persistence.Table;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class FeedSubscription extends AbstractModel { public class FeedSubscription extends AbstractModel {
@ManyToOne @ManyToOne(fetch = FetchType.EAGER)
private User user; private User user;
@ManyToOne @ManyToOne(fetch = FetchType.EAGER)
private Feed feed; private Feed feed;
@Column(length = 128) @Column(length = 128)

View File

@@ -1,5 +1,7 @@
package com.commafeed.frontend.rest; package com.commafeed.frontend.rest;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@@ -66,7 +68,9 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
WebApplicationException { WebApplicationException {
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString() httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString()
+ ";charset=UTF-8"); + ";charset=UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); OutputStreamWriter writer = new OutputStreamWriter(
new BufferedOutputStream(entityStream), UTF_8);
try { try {
Type jsonType; Type jsonType;
if (type.equals(genericType)) { if (type.equals(genericType)) {
@@ -85,8 +89,8 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
Annotation[] annotations, MediaType mediaType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException { throws IOException, WebApplicationException {
InputStreamReader streamReader = new InputStreamReader(entityStream, InputStreamReader reader = new InputStreamReader(
UTF_8); new BufferedInputStream(entityStream), UTF_8);
try { try {
Type jsonType; Type jsonType;
if (type.equals(genericType)) { if (type.equals(genericType)) {
@@ -94,9 +98,9 @@ public class JSONMessageBodyReaderWriter implements MessageBodyWriter<Object>,
} else { } else {
jsonType = genericType; jsonType = genericType;
} }
return getGson().fromJson(streamReader, jsonType); return getGson().fromJson(reader, jsonType);
} finally { } finally {
streamReader.close(); reader.close();
} }
} }

View File

@@ -80,52 +80,50 @@ public class SubscriptionsREST extends AbstractREST {
@GET @GET
public Category getSubscriptions() { public Category getSubscriptions() {
Category root = new Category();
List<FeedCategory> categories = feedCategoryService.findAll(getUser()); List<FeedCategory> categories = feedCategoryService.findAll(getUser());
addChildren(categories, root); List<FeedSubscription> subscriptions = feedSubscriptionService
.findAll(getUser());
Category root = buildCategory(null, categories, subscriptions);
root.setId("all"); root.setId("all");
root.setName("All"); root.setName("All");
for (FeedSubscription subscription : feedSubscriptionService
.findWithoutCategories(getUser())) {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
sub.setMessage(subscription.getFeed().getMessage());
int size = feedEntryService.getEntries(subscription.getFeed(),
getUser(), false).size();
sub.setUnread(size);
root.getFeeds().add(sub);
}
return root; return root;
} }
private void addChildren(List<FeedCategory> categories, Category current) { Category buildCategory(Long id, List<FeedCategory> categories,
for (FeedCategory category : categories) { List<FeedSubscription> subscriptions) {
if ((category.getParent() == null && current.getId() == null) Category category = new Category();
|| (category.getParent() != null && (ObjectUtils.equals( category.setId(String.valueOf(id));
String.valueOf(category.getParent().getId()),
current.getId())))) { for (FeedCategory c : categories) {
Category child = new Category(); if ((id == null && c.getParent() == null)
child.setId(String.valueOf(category.getId())); || (c.getParent() != null && ObjectUtils.equals(c
child.setName(category.getName()); .getParent().getId(), id))) {
addChildren(categories, child); Category child = buildCategory(c.getId(), categories,
for (FeedSubscription subscription : category subscriptions);
.getSubscriptions()) { child.setId(String.valueOf(c.getId()));
Subscription sub = new Subscription(); child.setName(c.getName());
sub.setId(subscription.getId()); category.getChildren().add(child);
sub.setName(subscription.getTitle());
sub.setMessage(subscription.getFeed().getMessage());
int size = feedEntryService.getEntries(
subscription.getFeed(), getUser(), false).size();
sub.setUnread(size);
child.getFeeds().add(sub);
}
current.getChildren().add(child);
} }
} }
for (FeedSubscription subscription : subscriptions) {
if ((id == null && subscription.getCategory() == null)
|| (subscription.getCategory() != null && ObjectUtils
.equals(subscription.getCategory().getId(), id))) {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
sub.setMessage(subscription.getFeed().getMessage());
int size = feedEntryService.getEntries(subscription.getFeed(),
getUser(), false).size();
sub.setUnread(size);
category.getFeeds().add(sub);
}
}
return category;
} }
} }